10b57cec5SDimitry Andric //===- MachineSink.cpp - Sinking for machine instructions -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass moves instructions into successor blocks when possible, so that
100b57cec5SDimitry Andric // they aren't executed on paths where their results aren't needed.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric // This pass is not intended to be a replacement or a complete alternative
130b57cec5SDimitry Andric // for an LLVM-IR-level sinking pass. It is only designed to sink simple
140b57cec5SDimitry Andric // constructs that are not exposed before lowering and instruction selection.
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
170b57cec5SDimitry Andric 
18480093f4SDimitry Andric #include "llvm/ADT/DenseSet.h"
19*5f7ddb14SDimitry Andric #include "llvm/ADT/MapVector.h"
20480093f4SDimitry Andric #include "llvm/ADT/PointerIntPair.h"
210b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
220b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
230b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
240b57cec5SDimitry Andric #include "llvm/ADT/SparseBitVector.h"
250b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
260b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/MachinePostDominators.h"
370b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
38af732203SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
39af732203SDimitry Andric #include "llvm/CodeGen/RegisterPressure.h"
400b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
410b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
420b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
430b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
440b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
458bcb0991SDimitry Andric #include "llvm/IR/LLVMContext.h"
46480093f4SDimitry Andric #include "llvm/InitializePasses.h"
478bcb0991SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
480b57cec5SDimitry Andric #include "llvm/Pass.h"
490b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h"
500b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
510b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
520b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
530b57cec5SDimitry Andric #include <algorithm>
540b57cec5SDimitry Andric #include <cassert>
550b57cec5SDimitry Andric #include <cstdint>
560b57cec5SDimitry Andric #include <map>
570b57cec5SDimitry Andric #include <utility>
580b57cec5SDimitry Andric #include <vector>
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric using namespace llvm;
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric #define DEBUG_TYPE "machine-sink"
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric static cl::opt<bool>
650b57cec5SDimitry Andric SplitEdges("machine-sink-split",
660b57cec5SDimitry Andric            cl::desc("Split critical edges during machine sinking"),
670b57cec5SDimitry Andric            cl::init(true), cl::Hidden);
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric static cl::opt<bool>
700b57cec5SDimitry Andric UseBlockFreqInfo("machine-sink-bfi",
710b57cec5SDimitry Andric            cl::desc("Use block frequency info to find successors to sink"),
720b57cec5SDimitry Andric            cl::init(true), cl::Hidden);
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric static cl::opt<unsigned> SplitEdgeProbabilityThreshold(
750b57cec5SDimitry Andric     "machine-sink-split-probability-threshold",
760b57cec5SDimitry Andric     cl::desc(
770b57cec5SDimitry Andric         "Percentage threshold for splitting single-instruction critical edge. "
780b57cec5SDimitry Andric         "If the branch threshold is higher than this threshold, we allow "
790b57cec5SDimitry Andric         "speculative execution of up to 1 instruction to avoid branching to "
800b57cec5SDimitry Andric         "splitted critical edge"),
810b57cec5SDimitry Andric     cl::init(40), cl::Hidden);
820b57cec5SDimitry Andric 
83af732203SDimitry Andric static cl::opt<unsigned> SinkLoadInstsPerBlockThreshold(
84af732203SDimitry Andric     "machine-sink-load-instrs-threshold",
85af732203SDimitry Andric     cl::desc("Do not try to find alias store for a load if there is a in-path "
86af732203SDimitry Andric              "block whose instruction number is higher than this threshold."),
87af732203SDimitry Andric     cl::init(2000), cl::Hidden);
88af732203SDimitry Andric 
89af732203SDimitry Andric static cl::opt<unsigned> SinkLoadBlocksThreshold(
90af732203SDimitry Andric     "machine-sink-load-blocks-threshold",
91af732203SDimitry Andric     cl::desc("Do not try to find alias store for a load if the block number in "
92af732203SDimitry Andric              "the straight line is higher than this threshold."),
93af732203SDimitry Andric     cl::init(20), cl::Hidden);
94af732203SDimitry Andric 
95*5f7ddb14SDimitry Andric static cl::opt<bool>
96*5f7ddb14SDimitry Andric SinkInstsIntoLoop("sink-insts-to-avoid-spills",
97*5f7ddb14SDimitry Andric                   cl::desc("Sink instructions into loops to avoid "
98*5f7ddb14SDimitry Andric                            "register spills"),
99*5f7ddb14SDimitry Andric                   cl::init(false), cl::Hidden);
100*5f7ddb14SDimitry Andric 
101*5f7ddb14SDimitry Andric static cl::opt<unsigned> SinkIntoLoopLimit(
102*5f7ddb14SDimitry Andric     "machine-sink-loop-limit",
103*5f7ddb14SDimitry Andric     cl::desc("The maximum number of instructions considered for loop sinking."),
104*5f7ddb14SDimitry Andric     cl::init(50), cl::Hidden);
105*5f7ddb14SDimitry Andric 
1060b57cec5SDimitry Andric STATISTIC(NumSunk,      "Number of machine instructions sunk");
107*5f7ddb14SDimitry Andric STATISTIC(NumLoopSunk,  "Number of machine instructions sunk into a loop");
1080b57cec5SDimitry Andric STATISTIC(NumSplit,     "Number of critical edges split");
1090b57cec5SDimitry Andric STATISTIC(NumCoalesces, "Number of copies coalesced");
1100b57cec5SDimitry Andric STATISTIC(NumPostRACopySink, "Number of copies sunk after RA");
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric namespace {
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   class MachineSinking : public MachineFunctionPass {
1150b57cec5SDimitry Andric     const TargetInstrInfo *TII;
1160b57cec5SDimitry Andric     const TargetRegisterInfo *TRI;
1170b57cec5SDimitry Andric     MachineRegisterInfo  *MRI;     // Machine register information
1180b57cec5SDimitry Andric     MachineDominatorTree *DT;      // Machine dominator tree
1190b57cec5SDimitry Andric     MachinePostDominatorTree *PDT; // Machine post dominator tree
1200b57cec5SDimitry Andric     MachineLoopInfo *LI;
1215ffd83dbSDimitry Andric     MachineBlockFrequencyInfo *MBFI;
1220b57cec5SDimitry Andric     const MachineBranchProbabilityInfo *MBPI;
1230b57cec5SDimitry Andric     AliasAnalysis *AA;
124af732203SDimitry Andric     RegisterClassInfo RegClassInfo;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric     // Remember which edges have been considered for breaking.
1270b57cec5SDimitry Andric     SmallSet<std::pair<MachineBasicBlock*, MachineBasicBlock*>, 8>
1280b57cec5SDimitry Andric     CEBCandidates;
1290b57cec5SDimitry Andric     // Remember which edges we are about to split.
1300b57cec5SDimitry Andric     // This is different from CEBCandidates since those edges
1310b57cec5SDimitry Andric     // will be split.
1320b57cec5SDimitry Andric     SetVector<std::pair<MachineBasicBlock *, MachineBasicBlock *>> ToSplit;
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric     SparseBitVector<> RegsToClearKillFlags;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric     using AllSuccsCache =
1370b57cec5SDimitry Andric         std::map<MachineBasicBlock *, SmallVector<MachineBasicBlock *, 4>>;
1380b57cec5SDimitry Andric 
139480093f4SDimitry Andric     /// DBG_VALUE pointer and flag. The flag is true if this DBG_VALUE is
140480093f4SDimitry Andric     /// post-dominated by another DBG_VALUE of the same variable location.
141480093f4SDimitry Andric     /// This is necessary to detect sequences such as:
142480093f4SDimitry Andric     ///     %0 = someinst
143480093f4SDimitry Andric     ///     DBG_VALUE %0, !123, !DIExpression()
144480093f4SDimitry Andric     ///     %1 = anotherinst
145480093f4SDimitry Andric     ///     DBG_VALUE %1, !123, !DIExpression()
146480093f4SDimitry Andric     /// Where if %0 were to sink, the DBG_VAUE should not sink with it, as that
147480093f4SDimitry Andric     /// would re-order assignments.
148480093f4SDimitry Andric     using SeenDbgUser = PointerIntPair<MachineInstr *, 1>;
149480093f4SDimitry Andric 
150480093f4SDimitry Andric     /// Record of DBG_VALUE uses of vregs in a block, so that we can identify
151480093f4SDimitry Andric     /// debug instructions to sink.
152480093f4SDimitry Andric     SmallDenseMap<unsigned, TinyPtrVector<SeenDbgUser>> SeenDbgUsers;
153480093f4SDimitry Andric 
154480093f4SDimitry Andric     /// Record of debug variables that have had their locations set in the
155480093f4SDimitry Andric     /// current block.
156480093f4SDimitry Andric     DenseSet<DebugVariable> SeenDbgVars;
157480093f4SDimitry Andric 
158af732203SDimitry Andric     std::map<std::pair<MachineBasicBlock *, MachineBasicBlock *>, bool>
159af732203SDimitry Andric         HasStoreCache;
160af732203SDimitry Andric     std::map<std::pair<MachineBasicBlock *, MachineBasicBlock *>,
161af732203SDimitry Andric              std::vector<MachineInstr *>>
162af732203SDimitry Andric         StoreInstrCache;
163af732203SDimitry Andric 
164af732203SDimitry Andric     /// Cached BB's register pressure.
165af732203SDimitry Andric     std::map<MachineBasicBlock *, std::vector<unsigned>> CachedRegisterPressure;
166af732203SDimitry Andric 
1670b57cec5SDimitry Andric   public:
1680b57cec5SDimitry Andric     static char ID; // Pass identification
1690b57cec5SDimitry Andric 
MachineSinking()1700b57cec5SDimitry Andric     MachineSinking() : MachineFunctionPass(ID) {
1710b57cec5SDimitry Andric       initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
1720b57cec5SDimitry Andric     }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override;
1750b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const1760b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
1770b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
1780b57cec5SDimitry Andric       AU.addRequired<AAResultsWrapperPass>();
1790b57cec5SDimitry Andric       AU.addRequired<MachineDominatorTree>();
1800b57cec5SDimitry Andric       AU.addRequired<MachinePostDominatorTree>();
1810b57cec5SDimitry Andric       AU.addRequired<MachineLoopInfo>();
1820b57cec5SDimitry Andric       AU.addRequired<MachineBranchProbabilityInfo>();
1830b57cec5SDimitry Andric       AU.addPreserved<MachineLoopInfo>();
1840b57cec5SDimitry Andric       if (UseBlockFreqInfo)
1850b57cec5SDimitry Andric         AU.addRequired<MachineBlockFrequencyInfo>();
1860b57cec5SDimitry Andric     }
1870b57cec5SDimitry Andric 
releaseMemory()1880b57cec5SDimitry Andric     void releaseMemory() override {
1890b57cec5SDimitry Andric       CEBCandidates.clear();
1900b57cec5SDimitry Andric     }
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   private:
1930b57cec5SDimitry Andric     bool ProcessBlock(MachineBasicBlock &MBB);
194480093f4SDimitry Andric     void ProcessDbgInst(MachineInstr &MI);
1950b57cec5SDimitry Andric     bool isWorthBreakingCriticalEdge(MachineInstr &MI,
1960b57cec5SDimitry Andric                                      MachineBasicBlock *From,
1970b57cec5SDimitry Andric                                      MachineBasicBlock *To);
1980b57cec5SDimitry Andric 
199af732203SDimitry Andric     bool hasStoreBetween(MachineBasicBlock *From, MachineBasicBlock *To,
200af732203SDimitry Andric                          MachineInstr &MI);
201af732203SDimitry Andric 
2020b57cec5SDimitry Andric     /// Postpone the splitting of the given critical
2030b57cec5SDimitry Andric     /// edge (\p From, \p To).
2040b57cec5SDimitry Andric     ///
2050b57cec5SDimitry Andric     /// We do not split the edges on the fly. Indeed, this invalidates
2060b57cec5SDimitry Andric     /// the dominance information and thus triggers a lot of updates
2070b57cec5SDimitry Andric     /// of that information underneath.
2080b57cec5SDimitry Andric     /// Instead, we postpone all the splits after each iteration of
2090b57cec5SDimitry Andric     /// the main loop. That way, the information is at least valid
2100b57cec5SDimitry Andric     /// for the lifetime of an iteration.
2110b57cec5SDimitry Andric     ///
2120b57cec5SDimitry Andric     /// \return True if the edge is marked as toSplit, false otherwise.
2130b57cec5SDimitry Andric     /// False can be returned if, for instance, this is not profitable.
2140b57cec5SDimitry Andric     bool PostponeSplitCriticalEdge(MachineInstr &MI,
2150b57cec5SDimitry Andric                                    MachineBasicBlock *From,
2160b57cec5SDimitry Andric                                    MachineBasicBlock *To,
2170b57cec5SDimitry Andric                                    bool BreakPHIEdge);
2180b57cec5SDimitry Andric     bool SinkInstruction(MachineInstr &MI, bool &SawStore,
2190b57cec5SDimitry Andric                          AllSuccsCache &AllSuccessors);
220480093f4SDimitry Andric 
221480093f4SDimitry Andric     /// If we sink a COPY inst, some debug users of it's destination may no
222480093f4SDimitry Andric     /// longer be dominated by the COPY, and will eventually be dropped.
223480093f4SDimitry Andric     /// This is easily rectified by forwarding the non-dominated debug uses
224480093f4SDimitry Andric     /// to the copy source.
225480093f4SDimitry Andric     void SalvageUnsunkDebugUsersOfCopy(MachineInstr &,
226480093f4SDimitry Andric                                        MachineBasicBlock *TargetBlock);
227af732203SDimitry Andric     bool AllUsesDominatedByBlock(Register Reg, MachineBasicBlock *MBB,
228af732203SDimitry Andric                                  MachineBasicBlock *DefMBB, bool &BreakPHIEdge,
229af732203SDimitry Andric                                  bool &LocalUse) const;
2300b57cec5SDimitry Andric     MachineBasicBlock *FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
2310b57cec5SDimitry Andric                bool &BreakPHIEdge, AllSuccsCache &AllSuccessors);
232*5f7ddb14SDimitry Andric 
233*5f7ddb14SDimitry Andric     void FindLoopSinkCandidates(MachineLoop *L, MachineBasicBlock *BB,
234*5f7ddb14SDimitry Andric                                 SmallVectorImpl<MachineInstr *> &Candidates);
235*5f7ddb14SDimitry Andric     bool SinkIntoLoop(MachineLoop *L, MachineInstr &I);
236*5f7ddb14SDimitry Andric 
237af732203SDimitry Andric     bool isProfitableToSinkTo(Register Reg, MachineInstr &MI,
2380b57cec5SDimitry Andric                               MachineBasicBlock *MBB,
2390b57cec5SDimitry Andric                               MachineBasicBlock *SuccToSinkTo,
2400b57cec5SDimitry Andric                               AllSuccsCache &AllSuccessors);
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric     bool PerformTrivialForwardCoalescing(MachineInstr &MI,
2430b57cec5SDimitry Andric                                          MachineBasicBlock *MBB);
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric     SmallVector<MachineBasicBlock *, 4> &
2460b57cec5SDimitry Andric     GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
2470b57cec5SDimitry Andric                            AllSuccsCache &AllSuccessors) const;
248af732203SDimitry Andric 
249af732203SDimitry Andric     std::vector<unsigned> &getBBRegisterPressure(MachineBasicBlock &MBB);
2500b57cec5SDimitry Andric   };
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric } // end anonymous namespace
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric char MachineSinking::ID = 0;
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric char &llvm::MachineSinkingID = MachineSinking::ID;
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(MachineSinking, DEBUG_TYPE,
2590b57cec5SDimitry Andric                       "Machine code sinking", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)2600b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
2610b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
2620b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
2630b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
2640b57cec5SDimitry Andric INITIALIZE_PASS_END(MachineSinking, DEBUG_TYPE,
2650b57cec5SDimitry Andric                     "Machine code sinking", false, false)
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr &MI,
2680b57cec5SDimitry Andric                                                      MachineBasicBlock *MBB) {
2690b57cec5SDimitry Andric   if (!MI.isCopy())
2700b57cec5SDimitry Andric     return false;
2710b57cec5SDimitry Andric 
2728bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
2738bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
2748bcb0991SDimitry Andric   if (!Register::isVirtualRegister(SrcReg) ||
2758bcb0991SDimitry Andric       !Register::isVirtualRegister(DstReg) || !MRI->hasOneNonDBGUse(SrcReg))
2760b57cec5SDimitry Andric     return false;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
2790b57cec5SDimitry Andric   const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
2800b57cec5SDimitry Andric   if (SRC != DRC)
2810b57cec5SDimitry Andric     return false;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
2840b57cec5SDimitry Andric   if (DefMI->isCopyLike())
2850b57cec5SDimitry Andric     return false;
2860b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
2870b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "*** to: " << MI);
2880b57cec5SDimitry Andric   MRI->replaceRegWith(DstReg, SrcReg);
2890b57cec5SDimitry Andric   MI.eraseFromParent();
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   // Conservatively, clear any kill flags, since it's possible that they are no
2920b57cec5SDimitry Andric   // longer correct.
2930b57cec5SDimitry Andric   MRI->clearKillFlags(SrcReg);
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric   ++NumCoalesces;
2960b57cec5SDimitry Andric   return true;
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric /// AllUsesDominatedByBlock - Return true if all uses of the specified register
3000b57cec5SDimitry Andric /// occur in blocks dominated by the specified block. If any use is in the
3010b57cec5SDimitry Andric /// definition block, then return false since it is never legal to move def
3020b57cec5SDimitry Andric /// after uses.
AllUsesDominatedByBlock(Register Reg,MachineBasicBlock * MBB,MachineBasicBlock * DefMBB,bool & BreakPHIEdge,bool & LocalUse) const303af732203SDimitry Andric bool MachineSinking::AllUsesDominatedByBlock(Register Reg,
3040b57cec5SDimitry Andric                                              MachineBasicBlock *MBB,
3050b57cec5SDimitry Andric                                              MachineBasicBlock *DefMBB,
3060b57cec5SDimitry Andric                                              bool &BreakPHIEdge,
3070b57cec5SDimitry Andric                                              bool &LocalUse) const {
3088bcb0991SDimitry Andric   assert(Register::isVirtualRegister(Reg) && "Only makes sense for vregs");
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   // Ignore debug uses because debug info doesn't affect the code.
3110b57cec5SDimitry Andric   if (MRI->use_nodbg_empty(Reg))
3120b57cec5SDimitry Andric     return true;
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
3150b57cec5SDimitry Andric   // into and they are all PHI nodes. In this case, machine-sink must break
3160b57cec5SDimitry Andric   // the critical edge first. e.g.
3170b57cec5SDimitry Andric   //
318d65cd7a5SDimitry Andric   // %bb.1:
3190b57cec5SDimitry Andric   //   Predecessors according to CFG: %bb.0
3200b57cec5SDimitry Andric   //     ...
321d65cd7a5SDimitry Andric   //     %def = DEC64_32r %x, implicit-def dead %eflags
3220b57cec5SDimitry Andric   //     ...
3230b57cec5SDimitry Andric   //     JE_4 <%bb.37>, implicit %eflags
3240b57cec5SDimitry Andric   //   Successors according to CFG: %bb.37 %bb.2
3250b57cec5SDimitry Andric   //
326d65cd7a5SDimitry Andric   // %bb.2:
327d65cd7a5SDimitry Andric   //     %p = PHI %y, %bb.0, %def, %bb.1
3285ffd83dbSDimitry Andric   if (all_of(MRI->use_nodbg_operands(Reg), [&](MachineOperand &MO) {
3290b57cec5SDimitry Andric         MachineInstr *UseInst = MO.getParent();
330d65cd7a5SDimitry Andric         unsigned OpNo = UseInst->getOperandNo(&MO);
3310b57cec5SDimitry Andric         MachineBasicBlock *UseBlock = UseInst->getParent();
332d65cd7a5SDimitry Andric         return UseBlock == MBB && UseInst->isPHI() &&
333d65cd7a5SDimitry Andric                UseInst->getOperand(OpNo + 1).getMBB() == DefMBB;
334d65cd7a5SDimitry Andric       })) {
335d65cd7a5SDimitry Andric     BreakPHIEdge = true;
3360b57cec5SDimitry Andric     return true;
337d65cd7a5SDimitry Andric   }
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric   for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
3400b57cec5SDimitry Andric     // Determine the block of the use.
3410b57cec5SDimitry Andric     MachineInstr *UseInst = MO.getParent();
3420b57cec5SDimitry Andric     unsigned OpNo = &MO - &UseInst->getOperand(0);
3430b57cec5SDimitry Andric     MachineBasicBlock *UseBlock = UseInst->getParent();
3440b57cec5SDimitry Andric     if (UseInst->isPHI()) {
3450b57cec5SDimitry Andric       // PHI nodes use the operand in the predecessor block, not the block with
3460b57cec5SDimitry Andric       // the PHI.
3470b57cec5SDimitry Andric       UseBlock = UseInst->getOperand(OpNo+1).getMBB();
3480b57cec5SDimitry Andric     } else if (UseBlock == DefMBB) {
3490b57cec5SDimitry Andric       LocalUse = true;
3500b57cec5SDimitry Andric       return false;
3510b57cec5SDimitry Andric     }
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric     // Check that it dominates.
3540b57cec5SDimitry Andric     if (!DT->dominates(MBB, UseBlock))
3550b57cec5SDimitry Andric       return false;
3560b57cec5SDimitry Andric   }
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric   return true;
3590b57cec5SDimitry Andric }
3600b57cec5SDimitry Andric 
361*5f7ddb14SDimitry Andric /// Return true if this machine instruction loads from global offset table or
362*5f7ddb14SDimitry Andric /// constant pool.
mayLoadFromGOTOrConstantPool(MachineInstr & MI)363*5f7ddb14SDimitry Andric static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) {
364*5f7ddb14SDimitry Andric   assert(MI.mayLoad() && "Expected MI that loads!");
365*5f7ddb14SDimitry Andric 
366*5f7ddb14SDimitry Andric   // If we lost memory operands, conservatively assume that the instruction
367*5f7ddb14SDimitry Andric   // reads from everything..
368*5f7ddb14SDimitry Andric   if (MI.memoperands_empty())
369*5f7ddb14SDimitry Andric     return true;
370*5f7ddb14SDimitry Andric 
371*5f7ddb14SDimitry Andric   for (MachineMemOperand *MemOp : MI.memoperands())
372*5f7ddb14SDimitry Andric     if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
373*5f7ddb14SDimitry Andric       if (PSV->isGOT() || PSV->isConstantPool())
374*5f7ddb14SDimitry Andric         return true;
375*5f7ddb14SDimitry Andric 
376*5f7ddb14SDimitry Andric   return false;
377*5f7ddb14SDimitry Andric }
378*5f7ddb14SDimitry Andric 
FindLoopSinkCandidates(MachineLoop * L,MachineBasicBlock * BB,SmallVectorImpl<MachineInstr * > & Candidates)379*5f7ddb14SDimitry Andric void MachineSinking::FindLoopSinkCandidates(MachineLoop *L, MachineBasicBlock *BB,
380*5f7ddb14SDimitry Andric     SmallVectorImpl<MachineInstr *> &Candidates) {
381*5f7ddb14SDimitry Andric   for (auto &MI : *BB) {
382*5f7ddb14SDimitry Andric     LLVM_DEBUG(dbgs() << "LoopSink: Analysing candidate: " << MI);
383*5f7ddb14SDimitry Andric     if (!TII->shouldSink(MI)) {
384*5f7ddb14SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSink: Instruction not a candidate for this "
385*5f7ddb14SDimitry Andric                            "target\n");
386*5f7ddb14SDimitry Andric       continue;
387*5f7ddb14SDimitry Andric     }
388*5f7ddb14SDimitry Andric     if (!L->isLoopInvariant(MI)) {
389*5f7ddb14SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSink: Instruction is not loop invariant\n");
390*5f7ddb14SDimitry Andric       continue;
391*5f7ddb14SDimitry Andric     }
392*5f7ddb14SDimitry Andric     bool DontMoveAcrossStore = true;
393*5f7ddb14SDimitry Andric     if (!MI.isSafeToMove(AA, DontMoveAcrossStore)) {
394*5f7ddb14SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSink: Instruction not safe to move.\n");
395*5f7ddb14SDimitry Andric       continue;
396*5f7ddb14SDimitry Andric     }
397*5f7ddb14SDimitry Andric     if (MI.mayLoad() && !mayLoadFromGOTOrConstantPool(MI)) {
398*5f7ddb14SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSink: Dont sink GOT or constant pool loads\n");
399*5f7ddb14SDimitry Andric       continue;
400*5f7ddb14SDimitry Andric     }
401*5f7ddb14SDimitry Andric     if (MI.isConvergent())
402*5f7ddb14SDimitry Andric       continue;
403*5f7ddb14SDimitry Andric 
404*5f7ddb14SDimitry Andric     const MachineOperand &MO = MI.getOperand(0);
405*5f7ddb14SDimitry Andric     if (!MO.isReg() || !MO.getReg() || !MO.isDef())
406*5f7ddb14SDimitry Andric       continue;
407*5f7ddb14SDimitry Andric     if (!MRI->hasOneDef(MO.getReg()))
408*5f7ddb14SDimitry Andric       continue;
409*5f7ddb14SDimitry Andric 
410*5f7ddb14SDimitry Andric     LLVM_DEBUG(dbgs() << "LoopSink: Instruction added as candidate.\n");
411*5f7ddb14SDimitry Andric     Candidates.push_back(&MI);
412*5f7ddb14SDimitry Andric   }
413*5f7ddb14SDimitry Andric }
414*5f7ddb14SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)4150b57cec5SDimitry Andric bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
4160b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
4170b57cec5SDimitry Andric     return false;
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "******** Machine Sinking ********\n");
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   TII = MF.getSubtarget().getInstrInfo();
4220b57cec5SDimitry Andric   TRI = MF.getSubtarget().getRegisterInfo();
4230b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
4240b57cec5SDimitry Andric   DT = &getAnalysis<MachineDominatorTree>();
4250b57cec5SDimitry Andric   PDT = &getAnalysis<MachinePostDominatorTree>();
4260b57cec5SDimitry Andric   LI = &getAnalysis<MachineLoopInfo>();
4270b57cec5SDimitry Andric   MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfo>() : nullptr;
4280b57cec5SDimitry Andric   MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
4290b57cec5SDimitry Andric   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
430af732203SDimitry Andric   RegClassInfo.runOnMachineFunction(MF);
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric   bool EverMadeChange = false;
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric   while (true) {
4350b57cec5SDimitry Andric     bool MadeChange = false;
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric     // Process all basic blocks.
4380b57cec5SDimitry Andric     CEBCandidates.clear();
4390b57cec5SDimitry Andric     ToSplit.clear();
4400b57cec5SDimitry Andric     for (auto &MBB: MF)
4410b57cec5SDimitry Andric       MadeChange |= ProcessBlock(MBB);
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric     // If we have anything we marked as toSplit, split it now.
4440b57cec5SDimitry Andric     for (auto &Pair : ToSplit) {
4450b57cec5SDimitry Andric       auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, *this);
4460b57cec5SDimitry Andric       if (NewSucc != nullptr) {
4470b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " *** Splitting critical edge: "
4480b57cec5SDimitry Andric                           << printMBBReference(*Pair.first) << " -- "
4490b57cec5SDimitry Andric                           << printMBBReference(*NewSucc) << " -- "
4500b57cec5SDimitry Andric                           << printMBBReference(*Pair.second) << '\n');
451af732203SDimitry Andric         if (MBFI)
452af732203SDimitry Andric           MBFI->onEdgeSplit(*Pair.first, *NewSucc, *MBPI);
453af732203SDimitry Andric 
4540b57cec5SDimitry Andric         MadeChange = true;
4550b57cec5SDimitry Andric         ++NumSplit;
4560b57cec5SDimitry Andric       } else
4570b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " *** Not legal to break critical edge\n");
4580b57cec5SDimitry Andric     }
4590b57cec5SDimitry Andric     // If this iteration over the code changed anything, keep iterating.
4600b57cec5SDimitry Andric     if (!MadeChange) break;
4610b57cec5SDimitry Andric     EverMadeChange = true;
4620b57cec5SDimitry Andric   }
4630b57cec5SDimitry Andric 
464*5f7ddb14SDimitry Andric   if (SinkInstsIntoLoop) {
465*5f7ddb14SDimitry Andric     SmallVector<MachineLoop *, 8> Loops(LI->begin(), LI->end());
466*5f7ddb14SDimitry Andric     for (auto *L : Loops) {
467*5f7ddb14SDimitry Andric       MachineBasicBlock *Preheader = LI->findLoopPreheader(L);
468*5f7ddb14SDimitry Andric       if (!Preheader) {
469*5f7ddb14SDimitry Andric         LLVM_DEBUG(dbgs() << "LoopSink: Can't find preheader\n");
470*5f7ddb14SDimitry Andric         continue;
471*5f7ddb14SDimitry Andric       }
472*5f7ddb14SDimitry Andric       SmallVector<MachineInstr *, 8> Candidates;
473*5f7ddb14SDimitry Andric       FindLoopSinkCandidates(L, Preheader, Candidates);
474*5f7ddb14SDimitry Andric 
475*5f7ddb14SDimitry Andric       // Walk the candidates in reverse order so that we start with the use
476*5f7ddb14SDimitry Andric       // of a def-use chain, if there is any.
477*5f7ddb14SDimitry Andric       // TODO: Sort the candidates using a cost-model.
478*5f7ddb14SDimitry Andric       unsigned i = 0;
479*5f7ddb14SDimitry Andric       for (auto It = Candidates.rbegin(); It != Candidates.rend(); ++It) {
480*5f7ddb14SDimitry Andric         if (i++ == SinkIntoLoopLimit) {
481*5f7ddb14SDimitry Andric           LLVM_DEBUG(dbgs() << "LoopSink:   Limit reached of instructions to "
482*5f7ddb14SDimitry Andric                                "be analysed.");
483*5f7ddb14SDimitry Andric           break;
484*5f7ddb14SDimitry Andric         }
485*5f7ddb14SDimitry Andric 
486*5f7ddb14SDimitry Andric         MachineInstr *I = *It;
487*5f7ddb14SDimitry Andric         if (!SinkIntoLoop(L, *I))
488*5f7ddb14SDimitry Andric           break;
489*5f7ddb14SDimitry Andric         EverMadeChange = true;
490*5f7ddb14SDimitry Andric         ++NumLoopSunk;
491*5f7ddb14SDimitry Andric       }
492*5f7ddb14SDimitry Andric     }
493*5f7ddb14SDimitry Andric   }
494*5f7ddb14SDimitry Andric 
495af732203SDimitry Andric   HasStoreCache.clear();
496af732203SDimitry Andric   StoreInstrCache.clear();
497af732203SDimitry Andric 
4980b57cec5SDimitry Andric   // Now clear any kill flags for recorded registers.
4990b57cec5SDimitry Andric   for (auto I : RegsToClearKillFlags)
5000b57cec5SDimitry Andric     MRI->clearKillFlags(I);
5010b57cec5SDimitry Andric   RegsToClearKillFlags.clear();
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   return EverMadeChange;
5040b57cec5SDimitry Andric }
5050b57cec5SDimitry Andric 
ProcessBlock(MachineBasicBlock & MBB)5060b57cec5SDimitry Andric bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
5070b57cec5SDimitry Andric   // Can't sink anything out of a block that has less than two successors.
5080b57cec5SDimitry Andric   if (MBB.succ_size() <= 1 || MBB.empty()) return false;
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric   // Don't bother sinking code out of unreachable blocks. In addition to being
5110b57cec5SDimitry Andric   // unprofitable, it can also lead to infinite looping, because in an
5120b57cec5SDimitry Andric   // unreachable loop there may be nowhere to stop.
5130b57cec5SDimitry Andric   if (!DT->isReachableFromEntry(&MBB)) return false;
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   bool MadeChange = false;
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric   // Cache all successors, sorted by frequency info and loop depth.
5180b57cec5SDimitry Andric   AllSuccsCache AllSuccessors;
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   // Walk the basic block bottom-up.  Remember if we saw a store.
5210b57cec5SDimitry Andric   MachineBasicBlock::iterator I = MBB.end();
5220b57cec5SDimitry Andric   --I;
5230b57cec5SDimitry Andric   bool ProcessedBegin, SawStore = false;
5240b57cec5SDimitry Andric   do {
5250b57cec5SDimitry Andric     MachineInstr &MI = *I;  // The instruction to sink.
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric     // Predecrement I (if it's not begin) so that it isn't invalidated by
5280b57cec5SDimitry Andric     // sinking.
5290b57cec5SDimitry Andric     ProcessedBegin = I == MBB.begin();
5300b57cec5SDimitry Andric     if (!ProcessedBegin)
5310b57cec5SDimitry Andric       --I;
5320b57cec5SDimitry Andric 
533*5f7ddb14SDimitry Andric     if (MI.isDebugOrPseudoInstr()) {
534480093f4SDimitry Andric       if (MI.isDebugValue())
535480093f4SDimitry Andric         ProcessDbgInst(MI);
5360b57cec5SDimitry Andric       continue;
537480093f4SDimitry Andric     }
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric     bool Joined = PerformTrivialForwardCoalescing(MI, &MBB);
5400b57cec5SDimitry Andric     if (Joined) {
5410b57cec5SDimitry Andric       MadeChange = true;
5420b57cec5SDimitry Andric       continue;
5430b57cec5SDimitry Andric     }
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric     if (SinkInstruction(MI, SawStore, AllSuccessors)) {
5460b57cec5SDimitry Andric       ++NumSunk;
5470b57cec5SDimitry Andric       MadeChange = true;
5480b57cec5SDimitry Andric     }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric     // If we just processed the first instruction in the block, we're done.
5510b57cec5SDimitry Andric   } while (!ProcessedBegin);
5520b57cec5SDimitry Andric 
553480093f4SDimitry Andric   SeenDbgUsers.clear();
554480093f4SDimitry Andric   SeenDbgVars.clear();
555af732203SDimitry Andric   // recalculate the bb register pressure after sinking one BB.
556af732203SDimitry Andric   CachedRegisterPressure.clear();
557480093f4SDimitry Andric 
5580b57cec5SDimitry Andric   return MadeChange;
5590b57cec5SDimitry Andric }
5600b57cec5SDimitry Andric 
ProcessDbgInst(MachineInstr & MI)561480093f4SDimitry Andric void MachineSinking::ProcessDbgInst(MachineInstr &MI) {
562480093f4SDimitry Andric   // When we see DBG_VALUEs for registers, record any vreg it reads, so that
563480093f4SDimitry Andric   // we know what to sink if the vreg def sinks.
564480093f4SDimitry Andric   assert(MI.isDebugValue() && "Expected DBG_VALUE for processing");
565480093f4SDimitry Andric 
566480093f4SDimitry Andric   DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
567480093f4SDimitry Andric                     MI.getDebugLoc()->getInlinedAt());
568af732203SDimitry Andric   bool SeenBefore = SeenDbgVars.contains(Var);
569480093f4SDimitry Andric 
570*5f7ddb14SDimitry Andric   for (MachineOperand &MO : MI.debug_operands()) {
571480093f4SDimitry Andric     if (MO.isReg() && MO.getReg().isVirtual())
572480093f4SDimitry Andric       SeenDbgUsers[MO.getReg()].push_back(SeenDbgUser(&MI, SeenBefore));
573*5f7ddb14SDimitry Andric   }
574480093f4SDimitry Andric 
575480093f4SDimitry Andric   // Record the variable for any DBG_VALUE, to avoid re-ordering any of them.
576480093f4SDimitry Andric   SeenDbgVars.insert(Var);
577480093f4SDimitry Andric }
578480093f4SDimitry Andric 
isWorthBreakingCriticalEdge(MachineInstr & MI,MachineBasicBlock * From,MachineBasicBlock * To)5790b57cec5SDimitry Andric bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr &MI,
5800b57cec5SDimitry Andric                                                  MachineBasicBlock *From,
5810b57cec5SDimitry Andric                                                  MachineBasicBlock *To) {
5820b57cec5SDimitry Andric   // FIXME: Need much better heuristics.
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric   // If the pass has already considered breaking this edge (during this pass
5850b57cec5SDimitry Andric   // through the function), then let's go ahead and break it. This means
5860b57cec5SDimitry Andric   // sinking multiple "cheap" instructions into the same block.
5870b57cec5SDimitry Andric   if (!CEBCandidates.insert(std::make_pair(From, To)).second)
5880b57cec5SDimitry Andric     return true;
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric   if (!MI.isCopy() && !TII->isAsCheapAsAMove(MI))
5910b57cec5SDimitry Andric     return true;
5920b57cec5SDimitry Andric 
5930b57cec5SDimitry Andric   if (From->isSuccessor(To) && MBPI->getEdgeProbability(From, To) <=
5940b57cec5SDimitry Andric       BranchProbability(SplitEdgeProbabilityThreshold, 100))
5950b57cec5SDimitry Andric     return true;
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric   // MI is cheap, we probably don't want to break the critical edge for it.
5980b57cec5SDimitry Andric   // However, if this would allow some definitions of its source operands
5990b57cec5SDimitry Andric   // to be sunk then it's probably worth it.
6000b57cec5SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
6010b57cec5SDimitry Andric     const MachineOperand &MO = MI.getOperand(i);
6020b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isUse())
6030b57cec5SDimitry Andric       continue;
6048bcb0991SDimitry Andric     Register Reg = MO.getReg();
6050b57cec5SDimitry Andric     if (Reg == 0)
6060b57cec5SDimitry Andric       continue;
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric     // We don't move live definitions of physical registers,
6090b57cec5SDimitry Andric     // so sinking their uses won't enable any opportunities.
6108bcb0991SDimitry Andric     if (Register::isPhysicalRegister(Reg))
6110b57cec5SDimitry Andric       continue;
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric     // If this instruction is the only user of a virtual register,
6140b57cec5SDimitry Andric     // check if breaking the edge will enable sinking
6150b57cec5SDimitry Andric     // both this instruction and the defining instruction.
6160b57cec5SDimitry Andric     if (MRI->hasOneNonDBGUse(Reg)) {
6170b57cec5SDimitry Andric       // If the definition resides in same MBB,
6180b57cec5SDimitry Andric       // claim it's likely we can sink these together.
6190b57cec5SDimitry Andric       // If definition resides elsewhere, we aren't
6200b57cec5SDimitry Andric       // blocking it from being sunk so don't break the edge.
6210b57cec5SDimitry Andric       MachineInstr *DefMI = MRI->getVRegDef(Reg);
6220b57cec5SDimitry Andric       if (DefMI->getParent() == MI.getParent())
6230b57cec5SDimitry Andric         return true;
6240b57cec5SDimitry Andric     }
6250b57cec5SDimitry Andric   }
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric   return false;
6280b57cec5SDimitry Andric }
6290b57cec5SDimitry Andric 
PostponeSplitCriticalEdge(MachineInstr & MI,MachineBasicBlock * FromBB,MachineBasicBlock * ToBB,bool BreakPHIEdge)6300b57cec5SDimitry Andric bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr &MI,
6310b57cec5SDimitry Andric                                                MachineBasicBlock *FromBB,
6320b57cec5SDimitry Andric                                                MachineBasicBlock *ToBB,
6330b57cec5SDimitry Andric                                                bool BreakPHIEdge) {
6340b57cec5SDimitry Andric   if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
6350b57cec5SDimitry Andric     return false;
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric   // Avoid breaking back edge. From == To means backedge for single BB loop.
6380b57cec5SDimitry Andric   if (!SplitEdges || FromBB == ToBB)
6390b57cec5SDimitry Andric     return false;
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric   // Check for backedges of more "complex" loops.
6420b57cec5SDimitry Andric   if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
6430b57cec5SDimitry Andric       LI->isLoopHeader(ToBB))
6440b57cec5SDimitry Andric     return false;
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric   // It's not always legal to break critical edges and sink the computation
6470b57cec5SDimitry Andric   // to the edge.
6480b57cec5SDimitry Andric   //
6490b57cec5SDimitry Andric   // %bb.1:
6500b57cec5SDimitry Andric   // v1024
6510b57cec5SDimitry Andric   // Beq %bb.3
6520b57cec5SDimitry Andric   // <fallthrough>
6530b57cec5SDimitry Andric   // %bb.2:
6540b57cec5SDimitry Andric   // ... no uses of v1024
6550b57cec5SDimitry Andric   // <fallthrough>
6560b57cec5SDimitry Andric   // %bb.3:
6570b57cec5SDimitry Andric   // ...
6580b57cec5SDimitry Andric   //       = v1024
6590b57cec5SDimitry Andric   //
6600b57cec5SDimitry Andric   // If %bb.1 -> %bb.3 edge is broken and computation of v1024 is inserted:
6610b57cec5SDimitry Andric   //
6620b57cec5SDimitry Andric   // %bb.1:
6630b57cec5SDimitry Andric   // ...
6640b57cec5SDimitry Andric   // Bne %bb.2
6650b57cec5SDimitry Andric   // %bb.4:
6660b57cec5SDimitry Andric   // v1024 =
6670b57cec5SDimitry Andric   // B %bb.3
6680b57cec5SDimitry Andric   // %bb.2:
6690b57cec5SDimitry Andric   // ... no uses of v1024
6700b57cec5SDimitry Andric   // <fallthrough>
6710b57cec5SDimitry Andric   // %bb.3:
6720b57cec5SDimitry Andric   // ...
6730b57cec5SDimitry Andric   //       = v1024
6740b57cec5SDimitry Andric   //
6750b57cec5SDimitry Andric   // This is incorrect since v1024 is not computed along the %bb.1->%bb.2->%bb.3
6760b57cec5SDimitry Andric   // flow. We need to ensure the new basic block where the computation is
6770b57cec5SDimitry Andric   // sunk to dominates all the uses.
6780b57cec5SDimitry Andric   // It's only legal to break critical edge and sink the computation to the
6790b57cec5SDimitry Andric   // new block if all the predecessors of "To", except for "From", are
6800b57cec5SDimitry Andric   // not dominated by "From". Given SSA property, this means these
6810b57cec5SDimitry Andric   // predecessors are dominated by "To".
6820b57cec5SDimitry Andric   //
6830b57cec5SDimitry Andric   // There is no need to do this check if all the uses are PHI nodes. PHI
6840b57cec5SDimitry Andric   // sources are only defined on the specific predecessor edges.
6850b57cec5SDimitry Andric   if (!BreakPHIEdge) {
6860b57cec5SDimitry Andric     for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
6870b57cec5SDimitry Andric            E = ToBB->pred_end(); PI != E; ++PI) {
6880b57cec5SDimitry Andric       if (*PI == FromBB)
6890b57cec5SDimitry Andric         continue;
6900b57cec5SDimitry Andric       if (!DT->dominates(ToBB, *PI))
6910b57cec5SDimitry Andric         return false;
6920b57cec5SDimitry Andric     }
6930b57cec5SDimitry Andric   }
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric   ToSplit.insert(std::make_pair(FromBB, ToBB));
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric   return true;
6980b57cec5SDimitry Andric }
6990b57cec5SDimitry Andric 
700af732203SDimitry Andric std::vector<unsigned> &
getBBRegisterPressure(MachineBasicBlock & MBB)701af732203SDimitry Andric MachineSinking::getBBRegisterPressure(MachineBasicBlock &MBB) {
702af732203SDimitry Andric   // Currently to save compiling time, MBB's register pressure will not change
703af732203SDimitry Andric   // in one ProcessBlock iteration because of CachedRegisterPressure. but MBB's
704af732203SDimitry Andric   // register pressure is changed after sinking any instructions into it.
705af732203SDimitry Andric   // FIXME: need a accurate and cheap register pressure estiminate model here.
706af732203SDimitry Andric   auto RP = CachedRegisterPressure.find(&MBB);
707af732203SDimitry Andric   if (RP != CachedRegisterPressure.end())
708af732203SDimitry Andric     return RP->second;
709af732203SDimitry Andric 
710af732203SDimitry Andric   RegionPressure Pressure;
711af732203SDimitry Andric   RegPressureTracker RPTracker(Pressure);
712af732203SDimitry Andric 
713af732203SDimitry Andric   // Initialize the register pressure tracker.
714af732203SDimitry Andric   RPTracker.init(MBB.getParent(), &RegClassInfo, nullptr, &MBB, MBB.end(),
715af732203SDimitry Andric                  /*TrackLaneMasks*/ false, /*TrackUntiedDefs=*/true);
716af732203SDimitry Andric 
717af732203SDimitry Andric   for (MachineBasicBlock::iterator MII = MBB.instr_end(),
718af732203SDimitry Andric                                    MIE = MBB.instr_begin();
719af732203SDimitry Andric        MII != MIE; --MII) {
720af732203SDimitry Andric     MachineInstr &MI = *std::prev(MII);
721*5f7ddb14SDimitry Andric     if (MI.isDebugInstr() || MI.isPseudoProbe())
722af732203SDimitry Andric       continue;
723af732203SDimitry Andric     RegisterOperands RegOpers;
724af732203SDimitry Andric     RegOpers.collect(MI, *TRI, *MRI, false, false);
725af732203SDimitry Andric     RPTracker.recedeSkipDebugValues();
726af732203SDimitry Andric     assert(&*RPTracker.getPos() == &MI && "RPTracker sync error!");
727af732203SDimitry Andric     RPTracker.recede(RegOpers);
728af732203SDimitry Andric   }
729af732203SDimitry Andric 
730af732203SDimitry Andric   RPTracker.closeRegion();
731af732203SDimitry Andric   auto It = CachedRegisterPressure.insert(
732af732203SDimitry Andric       std::make_pair(&MBB, RPTracker.getPressure().MaxSetPressure));
733af732203SDimitry Andric   return It.first->second;
734af732203SDimitry Andric }
735af732203SDimitry Andric 
7360b57cec5SDimitry Andric /// isProfitableToSinkTo - Return true if it is profitable to sink MI.
isProfitableToSinkTo(Register Reg,MachineInstr & MI,MachineBasicBlock * MBB,MachineBasicBlock * SuccToSinkTo,AllSuccsCache & AllSuccessors)737af732203SDimitry Andric bool MachineSinking::isProfitableToSinkTo(Register Reg, MachineInstr &MI,
7380b57cec5SDimitry Andric                                           MachineBasicBlock *MBB,
7390b57cec5SDimitry Andric                                           MachineBasicBlock *SuccToSinkTo,
7400b57cec5SDimitry Andric                                           AllSuccsCache &AllSuccessors) {
7410b57cec5SDimitry Andric   assert (SuccToSinkTo && "Invalid SinkTo Candidate BB");
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric   if (MBB == SuccToSinkTo)
7440b57cec5SDimitry Andric     return false;
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric   // It is profitable if SuccToSinkTo does not post dominate current block.
7470b57cec5SDimitry Andric   if (!PDT->dominates(SuccToSinkTo, MBB))
7480b57cec5SDimitry Andric     return true;
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric   // It is profitable to sink an instruction from a deeper loop to a shallower
7510b57cec5SDimitry Andric   // loop, even if the latter post-dominates the former (PR21115).
7520b57cec5SDimitry Andric   if (LI->getLoopDepth(MBB) > LI->getLoopDepth(SuccToSinkTo))
7530b57cec5SDimitry Andric     return true;
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric   // Check if only use in post dominated block is PHI instruction.
7560b57cec5SDimitry Andric   bool NonPHIUse = false;
7570b57cec5SDimitry Andric   for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
7580b57cec5SDimitry Andric     MachineBasicBlock *UseBlock = UseInst.getParent();
7590b57cec5SDimitry Andric     if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
7600b57cec5SDimitry Andric       NonPHIUse = true;
7610b57cec5SDimitry Andric   }
7620b57cec5SDimitry Andric   if (!NonPHIUse)
7630b57cec5SDimitry Andric     return true;
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric   // If SuccToSinkTo post dominates then also it may be profitable if MI
7660b57cec5SDimitry Andric   // can further profitably sinked into another block in next round.
7670b57cec5SDimitry Andric   bool BreakPHIEdge = false;
7680b57cec5SDimitry Andric   // FIXME - If finding successor is compile time expensive then cache results.
7690b57cec5SDimitry Andric   if (MachineBasicBlock *MBB2 =
7700b57cec5SDimitry Andric           FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge, AllSuccessors))
7710b57cec5SDimitry Andric     return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2, AllSuccessors);
7720b57cec5SDimitry Andric 
773af732203SDimitry Andric   MachineLoop *ML = LI->getLoopFor(MBB);
774af732203SDimitry Andric 
775af732203SDimitry Andric   // If the instruction is not inside a loop, it is not profitable to sink MI to
776af732203SDimitry Andric   // a post dominate block SuccToSinkTo.
777af732203SDimitry Andric   if (!ML)
7780b57cec5SDimitry Andric     return false;
779af732203SDimitry Andric 
780af732203SDimitry Andric   auto isRegisterPressureSetExceedLimit = [&](const TargetRegisterClass *RC) {
781af732203SDimitry Andric     unsigned Weight = TRI->getRegClassWeight(RC).RegWeight;
782af732203SDimitry Andric     const int *PS = TRI->getRegClassPressureSets(RC);
783af732203SDimitry Andric     // Get register pressure for block SuccToSinkTo.
784af732203SDimitry Andric     std::vector<unsigned> BBRegisterPressure =
785af732203SDimitry Andric         getBBRegisterPressure(*SuccToSinkTo);
786af732203SDimitry Andric     for (; *PS != -1; PS++)
787af732203SDimitry Andric       // check if any register pressure set exceeds limit in block SuccToSinkTo
788af732203SDimitry Andric       // after sinking.
789af732203SDimitry Andric       if (Weight + BBRegisterPressure[*PS] >=
790af732203SDimitry Andric           TRI->getRegPressureSetLimit(*MBB->getParent(), *PS))
791af732203SDimitry Andric         return true;
792af732203SDimitry Andric     return false;
793af732203SDimitry Andric   };
794af732203SDimitry Andric 
795af732203SDimitry Andric   // If this instruction is inside a loop and sinking this instruction can make
796af732203SDimitry Andric   // more registers live range shorten, it is still prifitable.
797af732203SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
798af732203SDimitry Andric     const MachineOperand &MO = MI.getOperand(i);
799af732203SDimitry Andric     // Ignore non-register operands.
800af732203SDimitry Andric     if (!MO.isReg())
801af732203SDimitry Andric       continue;
802af732203SDimitry Andric     Register Reg = MO.getReg();
803af732203SDimitry Andric     if (Reg == 0)
804af732203SDimitry Andric       continue;
805af732203SDimitry Andric 
806af732203SDimitry Andric     // Don't handle physical register.
807af732203SDimitry Andric     if (Register::isPhysicalRegister(Reg))
808af732203SDimitry Andric       return false;
809af732203SDimitry Andric 
810af732203SDimitry Andric     // Users for the defs are all dominated by SuccToSinkTo.
811af732203SDimitry Andric     if (MO.isDef()) {
812af732203SDimitry Andric       // This def register's live range is shortened after sinking.
813af732203SDimitry Andric       bool LocalUse = false;
814af732203SDimitry Andric       if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, BreakPHIEdge,
815af732203SDimitry Andric                                    LocalUse))
816af732203SDimitry Andric         return false;
817af732203SDimitry Andric     } else {
818af732203SDimitry Andric       MachineInstr *DefMI = MRI->getVRegDef(Reg);
819af732203SDimitry Andric       // DefMI is defined outside of loop. There should be no live range
820af732203SDimitry Andric       // impact for this operand. Defination outside of loop means:
821af732203SDimitry Andric       // 1: defination is outside of loop.
822af732203SDimitry Andric       // 2: defination is in this loop, but it is a PHI in the loop header.
823af732203SDimitry Andric       if (LI->getLoopFor(DefMI->getParent()) != ML ||
824af732203SDimitry Andric           (DefMI->isPHI() && LI->isLoopHeader(DefMI->getParent())))
825af732203SDimitry Andric         continue;
826af732203SDimitry Andric       // The DefMI is defined inside the loop.
827af732203SDimitry Andric       // If sinking this operand makes some register pressure set exceed limit,
828af732203SDimitry Andric       // it is not profitable.
829af732203SDimitry Andric       if (isRegisterPressureSetExceedLimit(MRI->getRegClass(Reg))) {
830af732203SDimitry Andric         LLVM_DEBUG(dbgs() << "register pressure exceed limit, not profitable.");
831af732203SDimitry Andric         return false;
832af732203SDimitry Andric       }
833af732203SDimitry Andric     }
834af732203SDimitry Andric   }
835af732203SDimitry Andric 
836af732203SDimitry Andric   // If MI is in loop and all its operands are alive across the whole loop or if
837af732203SDimitry Andric   // no operand sinking make register pressure set exceed limit, it is
838af732203SDimitry Andric   // profitable to sink MI.
839af732203SDimitry Andric   return true;
8400b57cec5SDimitry Andric }
8410b57cec5SDimitry Andric 
8420b57cec5SDimitry Andric /// Get the sorted sequence of successors for this MachineBasicBlock, possibly
8430b57cec5SDimitry Andric /// computing it if it was not already cached.
8440b57cec5SDimitry Andric SmallVector<MachineBasicBlock *, 4> &
GetAllSortedSuccessors(MachineInstr & MI,MachineBasicBlock * MBB,AllSuccsCache & AllSuccessors) const8450b57cec5SDimitry Andric MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
8460b57cec5SDimitry Andric                                        AllSuccsCache &AllSuccessors) const {
8470b57cec5SDimitry Andric   // Do we have the sorted successors in cache ?
8480b57cec5SDimitry Andric   auto Succs = AllSuccessors.find(MBB);
8490b57cec5SDimitry Andric   if (Succs != AllSuccessors.end())
8500b57cec5SDimitry Andric     return Succs->second;
8510b57cec5SDimitry Andric 
852af732203SDimitry Andric   SmallVector<MachineBasicBlock *, 4> AllSuccs(MBB->successors());
8530b57cec5SDimitry Andric 
8540b57cec5SDimitry Andric   // Handle cases where sinking can happen but where the sink point isn't a
8550b57cec5SDimitry Andric   // successor. For example:
8560b57cec5SDimitry Andric   //
8570b57cec5SDimitry Andric   //   x = computation
8580b57cec5SDimitry Andric   //   if () {} else {}
8590b57cec5SDimitry Andric   //   use x
8600b57cec5SDimitry Andric   //
8615ffd83dbSDimitry Andric   for (MachineDomTreeNode *DTChild : DT->getNode(MBB)->children()) {
8620b57cec5SDimitry Andric     // DomTree children of MBB that have MBB as immediate dominator are added.
8630b57cec5SDimitry Andric     if (DTChild->getIDom()->getBlock() == MI.getParent() &&
8640b57cec5SDimitry Andric         // Skip MBBs already added to the AllSuccs vector above.
8650b57cec5SDimitry Andric         !MBB->isSuccessor(DTChild->getBlock()))
8660b57cec5SDimitry Andric       AllSuccs.push_back(DTChild->getBlock());
8675ffd83dbSDimitry Andric   }
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   // Sort Successors according to their loop depth or block frequency info.
8700b57cec5SDimitry Andric   llvm::stable_sort(
8710b57cec5SDimitry Andric       AllSuccs, [this](const MachineBasicBlock *L, const MachineBasicBlock *R) {
8720b57cec5SDimitry Andric         uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
8730b57cec5SDimitry Andric         uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
8740b57cec5SDimitry Andric         bool HasBlockFreq = LHSFreq != 0 && RHSFreq != 0;
8750b57cec5SDimitry Andric         return HasBlockFreq ? LHSFreq < RHSFreq
8760b57cec5SDimitry Andric                             : LI->getLoopDepth(L) < LI->getLoopDepth(R);
8770b57cec5SDimitry Andric       });
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   auto it = AllSuccessors.insert(std::make_pair(MBB, AllSuccs));
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric   return it.first->second;
8820b57cec5SDimitry Andric }
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric /// FindSuccToSinkTo - Find a successor to sink this instruction to.
8850b57cec5SDimitry Andric MachineBasicBlock *
FindSuccToSinkTo(MachineInstr & MI,MachineBasicBlock * MBB,bool & BreakPHIEdge,AllSuccsCache & AllSuccessors)8860b57cec5SDimitry Andric MachineSinking::FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
8870b57cec5SDimitry Andric                                  bool &BreakPHIEdge,
8880b57cec5SDimitry Andric                                  AllSuccsCache &AllSuccessors) {
8890b57cec5SDimitry Andric   assert (MBB && "Invalid MachineBasicBlock!");
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric   // Loop over all the operands of the specified instruction.  If there is
8920b57cec5SDimitry Andric   // anything we can't handle, bail out.
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric   // SuccToSinkTo - This is the successor to sink this instruction to, once we
8950b57cec5SDimitry Andric   // decide.
8960b57cec5SDimitry Andric   MachineBasicBlock *SuccToSinkTo = nullptr;
8970b57cec5SDimitry Andric   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
8980b57cec5SDimitry Andric     const MachineOperand &MO = MI.getOperand(i);
8990b57cec5SDimitry Andric     if (!MO.isReg()) continue;  // Ignore non-register operands.
9000b57cec5SDimitry Andric 
9018bcb0991SDimitry Andric     Register Reg = MO.getReg();
9020b57cec5SDimitry Andric     if (Reg == 0) continue;
9030b57cec5SDimitry Andric 
9048bcb0991SDimitry Andric     if (Register::isPhysicalRegister(Reg)) {
9050b57cec5SDimitry Andric       if (MO.isUse()) {
9060b57cec5SDimitry Andric         // If the physreg has no defs anywhere, it's just an ambient register
9070b57cec5SDimitry Andric         // and we can freely move its uses. Alternatively, if it's allocatable,
9080b57cec5SDimitry Andric         // it could get allocated to something with a def during allocation.
9090b57cec5SDimitry Andric         if (!MRI->isConstantPhysReg(Reg))
9100b57cec5SDimitry Andric           return nullptr;
9110b57cec5SDimitry Andric       } else if (!MO.isDead()) {
9120b57cec5SDimitry Andric         // A def that isn't dead. We can't move it.
9130b57cec5SDimitry Andric         return nullptr;
9140b57cec5SDimitry Andric       }
9150b57cec5SDimitry Andric     } else {
9160b57cec5SDimitry Andric       // Virtual register uses are always safe to sink.
9170b57cec5SDimitry Andric       if (MO.isUse()) continue;
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric       // If it's not safe to move defs of the register class, then abort.
9200b57cec5SDimitry Andric       if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
9210b57cec5SDimitry Andric         return nullptr;
9220b57cec5SDimitry Andric 
9230b57cec5SDimitry Andric       // Virtual register defs can only be sunk if all their uses are in blocks
9240b57cec5SDimitry Andric       // dominated by one of the successors.
9250b57cec5SDimitry Andric       if (SuccToSinkTo) {
9260b57cec5SDimitry Andric         // If a previous operand picked a block to sink to, then this operand
9270b57cec5SDimitry Andric         // must be sinkable to the same block.
9280b57cec5SDimitry Andric         bool LocalUse = false;
9290b57cec5SDimitry Andric         if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB,
9300b57cec5SDimitry Andric                                      BreakPHIEdge, LocalUse))
9310b57cec5SDimitry Andric           return nullptr;
9320b57cec5SDimitry Andric 
9330b57cec5SDimitry Andric         continue;
9340b57cec5SDimitry Andric       }
9350b57cec5SDimitry Andric 
9360b57cec5SDimitry Andric       // Otherwise, we should look at all the successors and decide which one
9370b57cec5SDimitry Andric       // we should sink to. If we have reliable block frequency information
9380b57cec5SDimitry Andric       // (frequency != 0) available, give successors with smaller frequencies
9390b57cec5SDimitry Andric       // higher priority, otherwise prioritize smaller loop depths.
9400b57cec5SDimitry Andric       for (MachineBasicBlock *SuccBlock :
9410b57cec5SDimitry Andric            GetAllSortedSuccessors(MI, MBB, AllSuccessors)) {
9420b57cec5SDimitry Andric         bool LocalUse = false;
9430b57cec5SDimitry Andric         if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
9440b57cec5SDimitry Andric                                     BreakPHIEdge, LocalUse)) {
9450b57cec5SDimitry Andric           SuccToSinkTo = SuccBlock;
9460b57cec5SDimitry Andric           break;
9470b57cec5SDimitry Andric         }
9480b57cec5SDimitry Andric         if (LocalUse)
9490b57cec5SDimitry Andric           // Def is used locally, it's never safe to move this def.
9500b57cec5SDimitry Andric           return nullptr;
9510b57cec5SDimitry Andric       }
9520b57cec5SDimitry Andric 
9530b57cec5SDimitry Andric       // If we couldn't find a block to sink to, ignore this instruction.
9540b57cec5SDimitry Andric       if (!SuccToSinkTo)
9550b57cec5SDimitry Andric         return nullptr;
9560b57cec5SDimitry Andric       if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo, AllSuccessors))
9570b57cec5SDimitry Andric         return nullptr;
9580b57cec5SDimitry Andric     }
9590b57cec5SDimitry Andric   }
9600b57cec5SDimitry Andric 
9610b57cec5SDimitry Andric   // It is not possible to sink an instruction into its own block.  This can
9620b57cec5SDimitry Andric   // happen with loops.
9630b57cec5SDimitry Andric   if (MBB == SuccToSinkTo)
9640b57cec5SDimitry Andric     return nullptr;
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric   // It's not safe to sink instructions to EH landing pad. Control flow into
9670b57cec5SDimitry Andric   // landing pad is implicitly defined.
9680b57cec5SDimitry Andric   if (SuccToSinkTo && SuccToSinkTo->isEHPad())
9690b57cec5SDimitry Andric     return nullptr;
9700b57cec5SDimitry Andric 
9715ffd83dbSDimitry Andric   // It ought to be okay to sink instructions into an INLINEASM_BR target, but
9725ffd83dbSDimitry Andric   // only if we make sure that MI occurs _before_ an INLINEASM_BR instruction in
9735ffd83dbSDimitry Andric   // the source block (which this code does not yet do). So for now, forbid
9745ffd83dbSDimitry Andric   // doing so.
9755ffd83dbSDimitry Andric   if (SuccToSinkTo && SuccToSinkTo->isInlineAsmBrIndirectTarget())
9765ffd83dbSDimitry Andric     return nullptr;
9775ffd83dbSDimitry Andric 
9780b57cec5SDimitry Andric   return SuccToSinkTo;
9790b57cec5SDimitry Andric }
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric /// Return true if MI is likely to be usable as a memory operation by the
9820b57cec5SDimitry Andric /// implicit null check optimization.
9830b57cec5SDimitry Andric ///
9840b57cec5SDimitry Andric /// This is a "best effort" heuristic, and should not be relied upon for
9850b57cec5SDimitry Andric /// correctness.  This returning true does not guarantee that the implicit null
9860b57cec5SDimitry Andric /// check optimization is legal over MI, and this returning false does not
9870b57cec5SDimitry Andric /// guarantee MI cannot possibly be used to do a null check.
SinkingPreventsImplicitNullCheck(MachineInstr & MI,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI)9880b57cec5SDimitry Andric static bool SinkingPreventsImplicitNullCheck(MachineInstr &MI,
9890b57cec5SDimitry Andric                                              const TargetInstrInfo *TII,
9900b57cec5SDimitry Andric                                              const TargetRegisterInfo *TRI) {
9910b57cec5SDimitry Andric   using MachineBranchPredicate = TargetInstrInfo::MachineBranchPredicate;
9920b57cec5SDimitry Andric 
9930b57cec5SDimitry Andric   auto *MBB = MI.getParent();
9940b57cec5SDimitry Andric   if (MBB->pred_size() != 1)
9950b57cec5SDimitry Andric     return false;
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   auto *PredMBB = *MBB->pred_begin();
9980b57cec5SDimitry Andric   auto *PredBB = PredMBB->getBasicBlock();
9990b57cec5SDimitry Andric 
10000b57cec5SDimitry Andric   // Frontends that don't use implicit null checks have no reason to emit
10010b57cec5SDimitry Andric   // branches with make.implicit metadata, and this function should always
10020b57cec5SDimitry Andric   // return false for them.
10030b57cec5SDimitry Andric   if (!PredBB ||
10040b57cec5SDimitry Andric       !PredBB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit))
10050b57cec5SDimitry Andric     return false;
10060b57cec5SDimitry Andric 
10070b57cec5SDimitry Andric   const MachineOperand *BaseOp;
10080b57cec5SDimitry Andric   int64_t Offset;
10095ffd83dbSDimitry Andric   bool OffsetIsScalable;
10105ffd83dbSDimitry Andric   if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable, TRI))
10110b57cec5SDimitry Andric     return false;
10120b57cec5SDimitry Andric 
10130b57cec5SDimitry Andric   if (!BaseOp->isReg())
10140b57cec5SDimitry Andric     return false;
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   if (!(MI.mayLoad() && !MI.isPredicable()))
10170b57cec5SDimitry Andric     return false;
10180b57cec5SDimitry Andric 
10190b57cec5SDimitry Andric   MachineBranchPredicate MBP;
10200b57cec5SDimitry Andric   if (TII->analyzeBranchPredicate(*PredMBB, MBP, false))
10210b57cec5SDimitry Andric     return false;
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric   return MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 &&
10240b57cec5SDimitry Andric          (MBP.Predicate == MachineBranchPredicate::PRED_NE ||
10250b57cec5SDimitry Andric           MBP.Predicate == MachineBranchPredicate::PRED_EQ) &&
10260b57cec5SDimitry Andric          MBP.LHS.getReg() == BaseOp->getReg();
10270b57cec5SDimitry Andric }
10280b57cec5SDimitry Andric 
1029480093f4SDimitry Andric /// If the sunk instruction is a copy, try to forward the copy instead of
1030480093f4SDimitry Andric /// leaving an 'undef' DBG_VALUE in the original location. Don't do this if
1031480093f4SDimitry Andric /// there's any subregister weirdness involved. Returns true if copy
1032480093f4SDimitry Andric /// propagation occurred.
attemptDebugCopyProp(MachineInstr & SinkInst,MachineInstr & DbgMI,Register Reg)1033*5f7ddb14SDimitry Andric static bool attemptDebugCopyProp(MachineInstr &SinkInst, MachineInstr &DbgMI,
1034*5f7ddb14SDimitry Andric                                  Register Reg) {
1035480093f4SDimitry Andric   const MachineRegisterInfo &MRI = SinkInst.getMF()->getRegInfo();
1036480093f4SDimitry Andric   const TargetInstrInfo &TII = *SinkInst.getMF()->getSubtarget().getInstrInfo();
1037480093f4SDimitry Andric 
1038480093f4SDimitry Andric   // Copy DBG_VALUE operand and set the original to undef. We then check to
1039480093f4SDimitry Andric   // see whether this is something that can be copy-forwarded. If it isn't,
1040480093f4SDimitry Andric   // continue around the loop.
1041480093f4SDimitry Andric 
1042480093f4SDimitry Andric   const MachineOperand *SrcMO = nullptr, *DstMO = nullptr;
1043480093f4SDimitry Andric   auto CopyOperands = TII.isCopyInstr(SinkInst);
1044480093f4SDimitry Andric   if (!CopyOperands)
1045480093f4SDimitry Andric     return false;
1046480093f4SDimitry Andric   SrcMO = CopyOperands->Source;
1047480093f4SDimitry Andric   DstMO = CopyOperands->Destination;
1048480093f4SDimitry Andric 
1049480093f4SDimitry Andric   // Check validity of forwarding this copy.
1050480093f4SDimitry Andric   bool PostRA = MRI.getNumVirtRegs() == 0;
1051480093f4SDimitry Andric 
1052480093f4SDimitry Andric   // Trying to forward between physical and virtual registers is too hard.
1053*5f7ddb14SDimitry Andric   if (Reg.isVirtual() != SrcMO->getReg().isVirtual())
1054480093f4SDimitry Andric     return false;
1055480093f4SDimitry Andric 
1056480093f4SDimitry Andric   // Only try virtual register copy-forwarding before regalloc, and physical
1057480093f4SDimitry Andric   // register copy-forwarding after regalloc.
1058*5f7ddb14SDimitry Andric   bool arePhysRegs = !Reg.isVirtual();
1059480093f4SDimitry Andric   if (arePhysRegs != PostRA)
1060480093f4SDimitry Andric     return false;
1061480093f4SDimitry Andric 
1062480093f4SDimitry Andric   // Pre-regalloc, only forward if all subregisters agree (or there are no
1063480093f4SDimitry Andric   // subregs at all). More analysis might recover some forwardable copies.
1064*5f7ddb14SDimitry Andric   if (!PostRA)
1065*5f7ddb14SDimitry Andric     for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg))
1066*5f7ddb14SDimitry Andric       if (DbgMO.getSubReg() != SrcMO->getSubReg() ||
1067*5f7ddb14SDimitry Andric           DbgMO.getSubReg() != DstMO->getSubReg())
1068480093f4SDimitry Andric         return false;
1069480093f4SDimitry Andric 
1070480093f4SDimitry Andric   // Post-regalloc, we may be sinking a DBG_VALUE of a sub or super-register
1071480093f4SDimitry Andric   // of this copy. Only forward the copy if the DBG_VALUE operand exactly
1072480093f4SDimitry Andric   // matches the copy destination.
1073*5f7ddb14SDimitry Andric   if (PostRA && Reg != DstMO->getReg())
1074480093f4SDimitry Andric     return false;
1075480093f4SDimitry Andric 
1076*5f7ddb14SDimitry Andric   for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg)) {
10775ffd83dbSDimitry Andric     DbgMO.setReg(SrcMO->getReg());
10785ffd83dbSDimitry Andric     DbgMO.setSubReg(SrcMO->getSubReg());
1079*5f7ddb14SDimitry Andric   }
1080480093f4SDimitry Andric   return true;
1081480093f4SDimitry Andric }
1082480093f4SDimitry Andric 
1083*5f7ddb14SDimitry Andric using MIRegs = std::pair<MachineInstr *, SmallVector<unsigned, 2>>;
1084480093f4SDimitry Andric /// Sink an instruction and its associated debug instructions.
performSink(MachineInstr & MI,MachineBasicBlock & SuccToSinkTo,MachineBasicBlock::iterator InsertPos,SmallVectorImpl<MIRegs> & DbgValuesToSink)10850b57cec5SDimitry Andric static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo,
10860b57cec5SDimitry Andric                         MachineBasicBlock::iterator InsertPos,
1087*5f7ddb14SDimitry Andric                         SmallVectorImpl<MIRegs> &DbgValuesToSink) {
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric   // If we cannot find a location to use (merge with), then we erase the debug
10900b57cec5SDimitry Andric   // location to prevent debug-info driven tools from potentially reporting
10910b57cec5SDimitry Andric   // wrong location information.
10920b57cec5SDimitry Andric   if (!SuccToSinkTo.empty() && InsertPos != SuccToSinkTo.end())
10930b57cec5SDimitry Andric     MI.setDebugLoc(DILocation::getMergedLocation(MI.getDebugLoc(),
10940b57cec5SDimitry Andric                                                  InsertPos->getDebugLoc()));
10950b57cec5SDimitry Andric   else
10960b57cec5SDimitry Andric     MI.setDebugLoc(DebugLoc());
10970b57cec5SDimitry Andric 
10980b57cec5SDimitry Andric   // Move the instruction.
10990b57cec5SDimitry Andric   MachineBasicBlock *ParentBlock = MI.getParent();
11000b57cec5SDimitry Andric   SuccToSinkTo.splice(InsertPos, ParentBlock, MI,
11010b57cec5SDimitry Andric                       ++MachineBasicBlock::iterator(MI));
11020b57cec5SDimitry Andric 
1103480093f4SDimitry Andric   // Sink a copy of debug users to the insert position. Mark the original
1104480093f4SDimitry Andric   // DBG_VALUE location as 'undef', indicating that any earlier variable
1105480093f4SDimitry Andric   // location should be terminated as we've optimised away the value at this
1106480093f4SDimitry Andric   // point.
1107*5f7ddb14SDimitry Andric   for (auto DbgValueToSink : DbgValuesToSink) {
1108*5f7ddb14SDimitry Andric     MachineInstr *DbgMI = DbgValueToSink.first;
1109*5f7ddb14SDimitry Andric     MachineInstr *NewDbgMI = DbgMI->getMF()->CloneMachineInstr(DbgMI);
1110480093f4SDimitry Andric     SuccToSinkTo.insert(InsertPos, NewDbgMI);
1111480093f4SDimitry Andric 
1112*5f7ddb14SDimitry Andric     bool PropagatedAllSunkOps = true;
1113*5f7ddb14SDimitry Andric     for (unsigned Reg : DbgValueToSink.second) {
1114*5f7ddb14SDimitry Andric       if (DbgMI->hasDebugOperandForReg(Reg)) {
1115*5f7ddb14SDimitry Andric         if (!attemptDebugCopyProp(MI, *DbgMI, Reg)) {
1116*5f7ddb14SDimitry Andric           PropagatedAllSunkOps = false;
1117*5f7ddb14SDimitry Andric           break;
1118*5f7ddb14SDimitry Andric         }
1119*5f7ddb14SDimitry Andric       }
1120*5f7ddb14SDimitry Andric     }
1121*5f7ddb14SDimitry Andric     if (!PropagatedAllSunkOps)
11225ffd83dbSDimitry Andric       DbgMI->setDebugValueUndef();
11230b57cec5SDimitry Andric   }
11240b57cec5SDimitry Andric }
11250b57cec5SDimitry Andric 
1126af732203SDimitry Andric /// hasStoreBetween - check if there is store betweeen straight line blocks From
1127af732203SDimitry Andric /// and To.
hasStoreBetween(MachineBasicBlock * From,MachineBasicBlock * To,MachineInstr & MI)1128af732203SDimitry Andric bool MachineSinking::hasStoreBetween(MachineBasicBlock *From,
1129af732203SDimitry Andric                                      MachineBasicBlock *To, MachineInstr &MI) {
1130af732203SDimitry Andric   // Make sure From and To are in straight line which means From dominates To
1131af732203SDimitry Andric   // and To post dominates From.
1132af732203SDimitry Andric   if (!DT->dominates(From, To) || !PDT->dominates(To, From))
1133af732203SDimitry Andric     return true;
1134af732203SDimitry Andric 
1135af732203SDimitry Andric   auto BlockPair = std::make_pair(From, To);
1136af732203SDimitry Andric 
1137af732203SDimitry Andric   // Does these two blocks pair be queried before and have a definite cached
1138af732203SDimitry Andric   // result?
1139af732203SDimitry Andric   if (HasStoreCache.find(BlockPair) != HasStoreCache.end())
1140af732203SDimitry Andric     return HasStoreCache[BlockPair];
1141af732203SDimitry Andric 
1142af732203SDimitry Andric   if (StoreInstrCache.find(BlockPair) != StoreInstrCache.end())
1143af732203SDimitry Andric     return llvm::any_of(StoreInstrCache[BlockPair], [&](MachineInstr *I) {
1144af732203SDimitry Andric       return I->mayAlias(AA, MI, false);
1145af732203SDimitry Andric     });
1146af732203SDimitry Andric 
1147af732203SDimitry Andric   bool SawStore = false;
1148af732203SDimitry Andric   bool HasAliasedStore = false;
1149af732203SDimitry Andric   DenseSet<MachineBasicBlock *> HandledBlocks;
1150af732203SDimitry Andric   DenseSet<MachineBasicBlock *> HandledDomBlocks;
1151af732203SDimitry Andric   // Go through all reachable blocks from From.
1152af732203SDimitry Andric   for (MachineBasicBlock *BB : depth_first(From)) {
1153af732203SDimitry Andric     // We insert the instruction at the start of block To, so no need to worry
1154af732203SDimitry Andric     // about stores inside To.
1155af732203SDimitry Andric     // Store in block From should be already considered when just enter function
1156af732203SDimitry Andric     // SinkInstruction.
1157af732203SDimitry Andric     if (BB == To || BB == From)
1158af732203SDimitry Andric       continue;
1159af732203SDimitry Andric 
1160af732203SDimitry Andric     // We already handle this BB in previous iteration.
1161af732203SDimitry Andric     if (HandledBlocks.count(BB))
1162af732203SDimitry Andric       continue;
1163af732203SDimitry Andric 
1164af732203SDimitry Andric     HandledBlocks.insert(BB);
1165af732203SDimitry Andric     // To post dominates BB, it must be a path from block From.
1166af732203SDimitry Andric     if (PDT->dominates(To, BB)) {
1167af732203SDimitry Andric       if (!HandledDomBlocks.count(BB))
1168af732203SDimitry Andric         HandledDomBlocks.insert(BB);
1169af732203SDimitry Andric 
1170af732203SDimitry Andric       // If this BB is too big or the block number in straight line between From
1171af732203SDimitry Andric       // and To is too big, stop searching to save compiling time.
1172af732203SDimitry Andric       if (BB->size() > SinkLoadInstsPerBlockThreshold ||
1173af732203SDimitry Andric           HandledDomBlocks.size() > SinkLoadBlocksThreshold) {
1174af732203SDimitry Andric         for (auto *DomBB : HandledDomBlocks) {
1175af732203SDimitry Andric           if (DomBB != BB && DT->dominates(DomBB, BB))
1176af732203SDimitry Andric             HasStoreCache[std::make_pair(DomBB, To)] = true;
1177af732203SDimitry Andric           else if(DomBB != BB && DT->dominates(BB, DomBB))
1178af732203SDimitry Andric             HasStoreCache[std::make_pair(From, DomBB)] = true;
1179af732203SDimitry Andric         }
1180af732203SDimitry Andric         HasStoreCache[BlockPair] = true;
1181af732203SDimitry Andric         return true;
1182af732203SDimitry Andric       }
1183af732203SDimitry Andric 
1184af732203SDimitry Andric       for (MachineInstr &I : *BB) {
1185af732203SDimitry Andric         // Treat as alias conservatively for a call or an ordered memory
1186af732203SDimitry Andric         // operation.
1187af732203SDimitry Andric         if (I.isCall() || I.hasOrderedMemoryRef()) {
1188af732203SDimitry Andric           for (auto *DomBB : HandledDomBlocks) {
1189af732203SDimitry Andric             if (DomBB != BB && DT->dominates(DomBB, BB))
1190af732203SDimitry Andric               HasStoreCache[std::make_pair(DomBB, To)] = true;
1191af732203SDimitry Andric             else if(DomBB != BB && DT->dominates(BB, DomBB))
1192af732203SDimitry Andric               HasStoreCache[std::make_pair(From, DomBB)] = true;
1193af732203SDimitry Andric           }
1194af732203SDimitry Andric           HasStoreCache[BlockPair] = true;
1195af732203SDimitry Andric           return true;
1196af732203SDimitry Andric         }
1197af732203SDimitry Andric 
1198af732203SDimitry Andric         if (I.mayStore()) {
1199af732203SDimitry Andric           SawStore = true;
1200af732203SDimitry Andric           // We still have chance to sink MI if all stores between are not
1201af732203SDimitry Andric           // aliased to MI.
1202af732203SDimitry Andric           // Cache all store instructions, so that we don't need to go through
1203af732203SDimitry Andric           // all From reachable blocks for next load instruction.
1204af732203SDimitry Andric           if (I.mayAlias(AA, MI, false))
1205af732203SDimitry Andric             HasAliasedStore = true;
1206af732203SDimitry Andric           StoreInstrCache[BlockPair].push_back(&I);
1207af732203SDimitry Andric         }
1208af732203SDimitry Andric       }
1209af732203SDimitry Andric     }
1210af732203SDimitry Andric   }
1211af732203SDimitry Andric   // If there is no store at all, cache the result.
1212af732203SDimitry Andric   if (!SawStore)
1213af732203SDimitry Andric     HasStoreCache[BlockPair] = false;
1214af732203SDimitry Andric   return HasAliasedStore;
1215af732203SDimitry Andric }
1216af732203SDimitry Andric 
1217*5f7ddb14SDimitry Andric /// Sink instructions into loops if profitable. This especially tries to prevent
1218*5f7ddb14SDimitry Andric /// register spills caused by register pressure if there is little to no
1219*5f7ddb14SDimitry Andric /// overhead moving instructions into loops.
SinkIntoLoop(MachineLoop * L,MachineInstr & I)1220*5f7ddb14SDimitry Andric bool MachineSinking::SinkIntoLoop(MachineLoop *L, MachineInstr &I) {
1221*5f7ddb14SDimitry Andric   LLVM_DEBUG(dbgs() << "LoopSink: Finding sink block for: " << I);
1222*5f7ddb14SDimitry Andric   MachineBasicBlock *Preheader = L->getLoopPreheader();
1223*5f7ddb14SDimitry Andric   assert(Preheader && "Loop sink needs a preheader block");
1224*5f7ddb14SDimitry Andric   MachineBasicBlock *SinkBlock = nullptr;
1225*5f7ddb14SDimitry Andric   bool CanSink = true;
1226*5f7ddb14SDimitry Andric   const MachineOperand &MO = I.getOperand(0);
1227*5f7ddb14SDimitry Andric 
1228*5f7ddb14SDimitry Andric   for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) {
1229*5f7ddb14SDimitry Andric     LLVM_DEBUG(dbgs() << "LoopSink:   Analysing use: " << MI);
1230*5f7ddb14SDimitry Andric     if (!L->contains(&MI)) {
1231*5f7ddb14SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSink:   Use not in loop, can't sink.\n");
1232*5f7ddb14SDimitry Andric       CanSink = false;
1233*5f7ddb14SDimitry Andric       break;
1234*5f7ddb14SDimitry Andric     }
1235*5f7ddb14SDimitry Andric 
1236*5f7ddb14SDimitry Andric     // FIXME: Come up with a proper cost model that estimates whether sinking
1237*5f7ddb14SDimitry Andric     // the instruction (and thus possibly executing it on every loop
1238*5f7ddb14SDimitry Andric     // iteration) is more expensive than a register.
1239*5f7ddb14SDimitry Andric     // For now assumes that copies are cheap and thus almost always worth it.
1240*5f7ddb14SDimitry Andric     if (!MI.isCopy()) {
1241*5f7ddb14SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSink:   Use is not a copy\n");
1242*5f7ddb14SDimitry Andric       CanSink = false;
1243*5f7ddb14SDimitry Andric       break;
1244*5f7ddb14SDimitry Andric     }
1245*5f7ddb14SDimitry Andric     if (!SinkBlock) {
1246*5f7ddb14SDimitry Andric       SinkBlock = MI.getParent();
1247*5f7ddb14SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSink:   Setting sink block to: "
1248*5f7ddb14SDimitry Andric                         << printMBBReference(*SinkBlock) << "\n");
1249*5f7ddb14SDimitry Andric       continue;
1250*5f7ddb14SDimitry Andric     }
1251*5f7ddb14SDimitry Andric     SinkBlock = DT->findNearestCommonDominator(SinkBlock, MI.getParent());
1252*5f7ddb14SDimitry Andric     if (!SinkBlock) {
1253*5f7ddb14SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSink:   Can't find nearest dominator\n");
1254*5f7ddb14SDimitry Andric       CanSink = false;
1255*5f7ddb14SDimitry Andric       break;
1256*5f7ddb14SDimitry Andric     }
1257*5f7ddb14SDimitry Andric     LLVM_DEBUG(dbgs() << "LoopSink:   Setting nearest common dom block: " <<
1258*5f7ddb14SDimitry Andric                printMBBReference(*SinkBlock) << "\n");
1259*5f7ddb14SDimitry Andric   }
1260*5f7ddb14SDimitry Andric 
1261*5f7ddb14SDimitry Andric   if (!CanSink) {
1262*5f7ddb14SDimitry Andric     LLVM_DEBUG(dbgs() << "LoopSink: Can't sink instruction.\n");
1263*5f7ddb14SDimitry Andric     return false;
1264*5f7ddb14SDimitry Andric   }
1265*5f7ddb14SDimitry Andric   if (!SinkBlock) {
1266*5f7ddb14SDimitry Andric     LLVM_DEBUG(dbgs() << "LoopSink: Not sinking, can't find sink block.\n");
1267*5f7ddb14SDimitry Andric     return false;
1268*5f7ddb14SDimitry Andric   }
1269*5f7ddb14SDimitry Andric   if (SinkBlock == Preheader) {
1270*5f7ddb14SDimitry Andric     LLVM_DEBUG(dbgs() << "LoopSink: Not sinking, sink block is the preheader\n");
1271*5f7ddb14SDimitry Andric     return false;
1272*5f7ddb14SDimitry Andric   }
1273*5f7ddb14SDimitry Andric   if (SinkBlock->size() > SinkLoadInstsPerBlockThreshold) {
1274*5f7ddb14SDimitry Andric     LLVM_DEBUG(dbgs() << "LoopSink: Not Sinking, block too large to analyse.\n");
1275*5f7ddb14SDimitry Andric     return false;
1276*5f7ddb14SDimitry Andric   }
1277*5f7ddb14SDimitry Andric 
1278*5f7ddb14SDimitry Andric   LLVM_DEBUG(dbgs() << "LoopSink: Sinking instruction!\n");
1279*5f7ddb14SDimitry Andric   SinkBlock->splice(SinkBlock->getFirstNonPHI(), Preheader, I);
1280*5f7ddb14SDimitry Andric 
1281*5f7ddb14SDimitry Andric   // The instruction is moved from its basic block, so do not retain the
1282*5f7ddb14SDimitry Andric   // debug information.
1283*5f7ddb14SDimitry Andric   assert(!I.isDebugInstr() && "Should not sink debug inst");
1284*5f7ddb14SDimitry Andric   I.setDebugLoc(DebugLoc());
1285*5f7ddb14SDimitry Andric   return true;
1286*5f7ddb14SDimitry Andric }
1287*5f7ddb14SDimitry Andric 
12880b57cec5SDimitry Andric /// SinkInstruction - Determine whether it is safe to sink the specified machine
12890b57cec5SDimitry Andric /// instruction out of its current block into a successor.
SinkInstruction(MachineInstr & MI,bool & SawStore,AllSuccsCache & AllSuccessors)12900b57cec5SDimitry Andric bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore,
12910b57cec5SDimitry Andric                                      AllSuccsCache &AllSuccessors) {
12920b57cec5SDimitry Andric   // Don't sink instructions that the target prefers not to sink.
12930b57cec5SDimitry Andric   if (!TII->shouldSink(MI))
12940b57cec5SDimitry Andric     return false;
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric   // Check if it's safe to move the instruction.
12970b57cec5SDimitry Andric   if (!MI.isSafeToMove(AA, SawStore))
12980b57cec5SDimitry Andric     return false;
12990b57cec5SDimitry Andric 
13000b57cec5SDimitry Andric   // Convergent operations may not be made control-dependent on additional
13010b57cec5SDimitry Andric   // values.
13020b57cec5SDimitry Andric   if (MI.isConvergent())
13030b57cec5SDimitry Andric     return false;
13040b57cec5SDimitry Andric 
13050b57cec5SDimitry Andric   // Don't break implicit null checks.  This is a performance heuristic, and not
13060b57cec5SDimitry Andric   // required for correctness.
13070b57cec5SDimitry Andric   if (SinkingPreventsImplicitNullCheck(MI, TII, TRI))
13080b57cec5SDimitry Andric     return false;
13090b57cec5SDimitry Andric 
13100b57cec5SDimitry Andric   // FIXME: This should include support for sinking instructions within the
13110b57cec5SDimitry Andric   // block they are currently in to shorten the live ranges.  We often get
13120b57cec5SDimitry Andric   // instructions sunk into the top of a large block, but it would be better to
13130b57cec5SDimitry Andric   // also sink them down before their first use in the block.  This xform has to
13140b57cec5SDimitry Andric   // be careful not to *increase* register pressure though, e.g. sinking
13150b57cec5SDimitry Andric   // "x = y + z" down if it kills y and z would increase the live ranges of y
13160b57cec5SDimitry Andric   // and z and only shrink the live range of x.
13170b57cec5SDimitry Andric 
13180b57cec5SDimitry Andric   bool BreakPHIEdge = false;
13190b57cec5SDimitry Andric   MachineBasicBlock *ParentBlock = MI.getParent();
13200b57cec5SDimitry Andric   MachineBasicBlock *SuccToSinkTo =
13210b57cec5SDimitry Andric       FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge, AllSuccessors);
13220b57cec5SDimitry Andric 
13230b57cec5SDimitry Andric   // If there are no outputs, it must have side-effects.
13240b57cec5SDimitry Andric   if (!SuccToSinkTo)
13250b57cec5SDimitry Andric     return false;
13260b57cec5SDimitry Andric 
13270b57cec5SDimitry Andric   // If the instruction to move defines a dead physical register which is live
13280b57cec5SDimitry Andric   // when leaving the basic block, don't move it because it could turn into a
13290b57cec5SDimitry Andric   // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
13300b57cec5SDimitry Andric   for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
13310b57cec5SDimitry Andric     const MachineOperand &MO = MI.getOperand(I);
13320b57cec5SDimitry Andric     if (!MO.isReg()) continue;
13338bcb0991SDimitry Andric     Register Reg = MO.getReg();
13348bcb0991SDimitry Andric     if (Reg == 0 || !Register::isPhysicalRegister(Reg))
13358bcb0991SDimitry Andric       continue;
13360b57cec5SDimitry Andric     if (SuccToSinkTo->isLiveIn(Reg))
13370b57cec5SDimitry Andric       return false;
13380b57cec5SDimitry Andric   }
13390b57cec5SDimitry Andric 
13400b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccToSinkTo);
13410b57cec5SDimitry Andric 
13420b57cec5SDimitry Andric   // If the block has multiple predecessors, this is a critical edge.
13430b57cec5SDimitry Andric   // Decide if we can sink along it or need to break the edge.
13440b57cec5SDimitry Andric   if (SuccToSinkTo->pred_size() > 1) {
13450b57cec5SDimitry Andric     // We cannot sink a load across a critical edge - there may be stores in
13460b57cec5SDimitry Andric     // other code paths.
13470b57cec5SDimitry Andric     bool TryBreak = false;
1348af732203SDimitry Andric     bool Store =
1349af732203SDimitry Andric         MI.mayLoad() ? hasStoreBetween(ParentBlock, SuccToSinkTo, MI) : true;
1350af732203SDimitry Andric     if (!MI.isSafeToMove(AA, Store)) {
13510b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
13520b57cec5SDimitry Andric       TryBreak = true;
13530b57cec5SDimitry Andric     }
13540b57cec5SDimitry Andric 
13550b57cec5SDimitry Andric     // We don't want to sink across a critical edge if we don't dominate the
13560b57cec5SDimitry Andric     // successor. We could be introducing calculations to new code paths.
13570b57cec5SDimitry Andric     if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
13580b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
13590b57cec5SDimitry Andric       TryBreak = true;
13600b57cec5SDimitry Andric     }
13610b57cec5SDimitry Andric 
13620b57cec5SDimitry Andric     // Don't sink instructions into a loop.
13630b57cec5SDimitry Andric     if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
13640b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " *** NOTE: Loop header found\n");
13650b57cec5SDimitry Andric       TryBreak = true;
13660b57cec5SDimitry Andric     }
13670b57cec5SDimitry Andric 
13680b57cec5SDimitry Andric     // Otherwise we are OK with sinking along a critical edge.
13690b57cec5SDimitry Andric     if (!TryBreak)
13700b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Sinking along critical edge.\n");
13710b57cec5SDimitry Andric     else {
13720b57cec5SDimitry Andric       // Mark this edge as to be split.
13730b57cec5SDimitry Andric       // If the edge can actually be split, the next iteration of the main loop
13740b57cec5SDimitry Andric       // will sink MI in the newly created block.
13750b57cec5SDimitry Andric       bool Status =
13760b57cec5SDimitry Andric         PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
13770b57cec5SDimitry Andric       if (!Status)
13780b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
13790b57cec5SDimitry Andric                              "break critical edge\n");
13800b57cec5SDimitry Andric       // The instruction will not be sunk this time.
13810b57cec5SDimitry Andric       return false;
13820b57cec5SDimitry Andric     }
13830b57cec5SDimitry Andric   }
13840b57cec5SDimitry Andric 
13850b57cec5SDimitry Andric   if (BreakPHIEdge) {
13860b57cec5SDimitry Andric     // BreakPHIEdge is true if all the uses are in the successor MBB being
13870b57cec5SDimitry Andric     // sunken into and they are all PHI nodes. In this case, machine-sink must
13880b57cec5SDimitry Andric     // break the critical edge first.
13890b57cec5SDimitry Andric     bool Status = PostponeSplitCriticalEdge(MI, ParentBlock,
13900b57cec5SDimitry Andric                                             SuccToSinkTo, BreakPHIEdge);
13910b57cec5SDimitry Andric     if (!Status)
13920b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
13930b57cec5SDimitry Andric                            "break critical edge\n");
13940b57cec5SDimitry Andric     // The instruction will not be sunk this time.
13950b57cec5SDimitry Andric     return false;
13960b57cec5SDimitry Andric   }
13970b57cec5SDimitry Andric 
13980b57cec5SDimitry Andric   // Determine where to insert into. Skip phi nodes.
13990b57cec5SDimitry Andric   MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
14000b57cec5SDimitry Andric   while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
14010b57cec5SDimitry Andric     ++InsertPos;
14020b57cec5SDimitry Andric 
1403480093f4SDimitry Andric   // Collect debug users of any vreg that this inst defines.
1404*5f7ddb14SDimitry Andric   SmallVector<MIRegs, 4> DbgUsersToSink;
1405480093f4SDimitry Andric   for (auto &MO : MI.operands()) {
1406480093f4SDimitry Andric     if (!MO.isReg() || !MO.isDef() || !MO.getReg().isVirtual())
1407480093f4SDimitry Andric       continue;
1408480093f4SDimitry Andric     if (!SeenDbgUsers.count(MO.getReg()))
1409480093f4SDimitry Andric       continue;
1410480093f4SDimitry Andric 
1411480093f4SDimitry Andric     // Sink any users that don't pass any other DBG_VALUEs for this variable.
1412480093f4SDimitry Andric     auto &Users = SeenDbgUsers[MO.getReg()];
1413480093f4SDimitry Andric     for (auto &User : Users) {
1414480093f4SDimitry Andric       MachineInstr *DbgMI = User.getPointer();
1415480093f4SDimitry Andric       if (User.getInt()) {
1416480093f4SDimitry Andric         // This DBG_VALUE would re-order assignments. If we can't copy-propagate
1417480093f4SDimitry Andric         // it, it can't be recovered. Set it undef.
1418*5f7ddb14SDimitry Andric         if (!attemptDebugCopyProp(MI, *DbgMI, MO.getReg()))
14195ffd83dbSDimitry Andric           DbgMI->setDebugValueUndef();
1420480093f4SDimitry Andric       } else {
1421*5f7ddb14SDimitry Andric         DbgUsersToSink.push_back(
1422*5f7ddb14SDimitry Andric             {DbgMI, SmallVector<unsigned, 2>(1, MO.getReg())});
1423480093f4SDimitry Andric       }
1424480093f4SDimitry Andric     }
1425480093f4SDimitry Andric   }
1426480093f4SDimitry Andric 
1427480093f4SDimitry Andric   // After sinking, some debug users may not be dominated any more. If possible,
1428480093f4SDimitry Andric   // copy-propagate their operands. As it's expensive, don't do this if there's
1429480093f4SDimitry Andric   // no debuginfo in the program.
1430480093f4SDimitry Andric   if (MI.getMF()->getFunction().getSubprogram() && MI.isCopy())
1431480093f4SDimitry Andric     SalvageUnsunkDebugUsersOfCopy(MI, SuccToSinkTo);
1432480093f4SDimitry Andric 
1433480093f4SDimitry Andric   performSink(MI, *SuccToSinkTo, InsertPos, DbgUsersToSink);
14340b57cec5SDimitry Andric 
14350b57cec5SDimitry Andric   // Conservatively, clear any kill flags, since it's possible that they are no
14360b57cec5SDimitry Andric   // longer correct.
14370b57cec5SDimitry Andric   // Note that we have to clear the kill flags for any register this instruction
14380b57cec5SDimitry Andric   // uses as we may sink over another instruction which currently kills the
14390b57cec5SDimitry Andric   // used registers.
14400b57cec5SDimitry Andric   for (MachineOperand &MO : MI.operands()) {
14410b57cec5SDimitry Andric     if (MO.isReg() && MO.isUse())
14420b57cec5SDimitry Andric       RegsToClearKillFlags.set(MO.getReg()); // Remember to clear kill flags.
14430b57cec5SDimitry Andric   }
14440b57cec5SDimitry Andric 
14450b57cec5SDimitry Andric   return true;
14460b57cec5SDimitry Andric }
14470b57cec5SDimitry Andric 
SalvageUnsunkDebugUsersOfCopy(MachineInstr & MI,MachineBasicBlock * TargetBlock)1448480093f4SDimitry Andric void MachineSinking::SalvageUnsunkDebugUsersOfCopy(
1449480093f4SDimitry Andric     MachineInstr &MI, MachineBasicBlock *TargetBlock) {
1450480093f4SDimitry Andric   assert(MI.isCopy());
1451480093f4SDimitry Andric   assert(MI.getOperand(1).isReg());
1452480093f4SDimitry Andric 
1453480093f4SDimitry Andric   // Enumerate all users of vreg operands that are def'd. Skip those that will
1454480093f4SDimitry Andric   // be sunk. For the rest, if they are not dominated by the block we will sink
1455480093f4SDimitry Andric   // MI into, propagate the copy source to them.
1456480093f4SDimitry Andric   SmallVector<MachineInstr *, 4> DbgDefUsers;
1457*5f7ddb14SDimitry Andric   SmallVector<Register, 4> DbgUseRegs;
1458480093f4SDimitry Andric   const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1459480093f4SDimitry Andric   for (auto &MO : MI.operands()) {
1460480093f4SDimitry Andric     if (!MO.isReg() || !MO.isDef() || !MO.getReg().isVirtual())
1461480093f4SDimitry Andric       continue;
1462*5f7ddb14SDimitry Andric     DbgUseRegs.push_back(MO.getReg());
1463480093f4SDimitry Andric     for (auto &User : MRI.use_instructions(MO.getReg())) {
1464480093f4SDimitry Andric       if (!User.isDebugValue() || DT->dominates(TargetBlock, User.getParent()))
1465480093f4SDimitry Andric         continue;
1466480093f4SDimitry Andric 
1467480093f4SDimitry Andric       // If is in same block, will either sink or be use-before-def.
1468480093f4SDimitry Andric       if (User.getParent() == MI.getParent())
1469480093f4SDimitry Andric         continue;
1470480093f4SDimitry Andric 
1471*5f7ddb14SDimitry Andric       assert(User.hasDebugOperandForReg(MO.getReg()) &&
1472*5f7ddb14SDimitry Andric              "DBG_VALUE user of vreg, but has no operand for it?");
1473480093f4SDimitry Andric       DbgDefUsers.push_back(&User);
1474480093f4SDimitry Andric     }
1475480093f4SDimitry Andric   }
1476480093f4SDimitry Andric 
1477480093f4SDimitry Andric   // Point the users of this copy that are no longer dominated, at the source
1478480093f4SDimitry Andric   // of the copy.
1479480093f4SDimitry Andric   for (auto *User : DbgDefUsers) {
1480*5f7ddb14SDimitry Andric     for (auto &Reg : DbgUseRegs) {
1481*5f7ddb14SDimitry Andric       for (auto &DbgOp : User->getDebugOperandsForReg(Reg)) {
1482*5f7ddb14SDimitry Andric         DbgOp.setReg(MI.getOperand(1).getReg());
1483*5f7ddb14SDimitry Andric         DbgOp.setSubReg(MI.getOperand(1).getSubReg());
1484*5f7ddb14SDimitry Andric       }
1485*5f7ddb14SDimitry Andric     }
1486480093f4SDimitry Andric   }
1487480093f4SDimitry Andric }
1488480093f4SDimitry Andric 
14890b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14900b57cec5SDimitry Andric // This pass is not intended to be a replacement or a complete alternative
14910b57cec5SDimitry Andric // for the pre-ra machine sink pass. It is only designed to sink COPY
14920b57cec5SDimitry Andric // instructions which should be handled after RA.
14930b57cec5SDimitry Andric //
14940b57cec5SDimitry Andric // This pass sinks COPY instructions into a successor block, if the COPY is not
14950b57cec5SDimitry Andric // used in the current block and the COPY is live-in to a single successor
14960b57cec5SDimitry Andric // (i.e., doesn't require the COPY to be duplicated).  This avoids executing the
14970b57cec5SDimitry Andric // copy on paths where their results aren't needed.  This also exposes
14980b57cec5SDimitry Andric // additional opportunites for dead copy elimination and shrink wrapping.
14990b57cec5SDimitry Andric //
15000b57cec5SDimitry Andric // These copies were either not handled by or are inserted after the MachineSink
15010b57cec5SDimitry Andric // pass. As an example of the former case, the MachineSink pass cannot sink
15020b57cec5SDimitry Andric // COPY instructions with allocatable source registers; for AArch64 these type
15030b57cec5SDimitry Andric // of copy instructions are frequently used to move function parameters (PhyReg)
15040b57cec5SDimitry Andric // into virtual registers in the entry block.
15050b57cec5SDimitry Andric //
15060b57cec5SDimitry Andric // For the machine IR below, this pass will sink %w19 in the entry into its
15070b57cec5SDimitry Andric // successor (%bb.1) because %w19 is only live-in in %bb.1.
15080b57cec5SDimitry Andric // %bb.0:
15090b57cec5SDimitry Andric //   %wzr = SUBSWri %w1, 1
15100b57cec5SDimitry Andric //   %w19 = COPY %w0
15110b57cec5SDimitry Andric //   Bcc 11, %bb.2
15120b57cec5SDimitry Andric // %bb.1:
15130b57cec5SDimitry Andric //   Live Ins: %w19
15140b57cec5SDimitry Andric //   BL @fun
15150b57cec5SDimitry Andric //   %w0 = ADDWrr %w0, %w19
15160b57cec5SDimitry Andric //   RET %w0
15170b57cec5SDimitry Andric // %bb.2:
15180b57cec5SDimitry Andric //   %w0 = COPY %wzr
15190b57cec5SDimitry Andric //   RET %w0
15200b57cec5SDimitry Andric // As we sink %w19 (CSR in AArch64) into %bb.1, the shrink-wrapping pass will be
15210b57cec5SDimitry Andric // able to see %bb.0 as a candidate.
15220b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15230b57cec5SDimitry Andric namespace {
15240b57cec5SDimitry Andric 
15250b57cec5SDimitry Andric class PostRAMachineSinking : public MachineFunctionPass {
15260b57cec5SDimitry Andric public:
15270b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
15280b57cec5SDimitry Andric 
15290b57cec5SDimitry Andric   static char ID;
PostRAMachineSinking()15300b57cec5SDimitry Andric   PostRAMachineSinking() : MachineFunctionPass(ID) {}
getPassName() const15310b57cec5SDimitry Andric   StringRef getPassName() const override { return "PostRA Machine Sink"; }
15320b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const15330b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
15340b57cec5SDimitry Andric     AU.setPreservesCFG();
15350b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
15360b57cec5SDimitry Andric   }
15370b57cec5SDimitry Andric 
getRequiredProperties() const15380b57cec5SDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
15390b57cec5SDimitry Andric     return MachineFunctionProperties().set(
15400b57cec5SDimitry Andric         MachineFunctionProperties::Property::NoVRegs);
15410b57cec5SDimitry Andric   }
15420b57cec5SDimitry Andric 
15430b57cec5SDimitry Andric private:
15440b57cec5SDimitry Andric   /// Track which register units have been modified and used.
15450b57cec5SDimitry Andric   LiveRegUnits ModifiedRegUnits, UsedRegUnits;
15460b57cec5SDimitry Andric 
15478bcb0991SDimitry Andric   /// Track DBG_VALUEs of (unmodified) register units. Each DBG_VALUE has an
1548*5f7ddb14SDimitry Andric   /// entry in this map for each unit it touches. The DBG_VALUE's entry
1549*5f7ddb14SDimitry Andric   /// consists of a pointer to the instruction itself, and a vector of registers
1550*5f7ddb14SDimitry Andric   /// referred to by the instruction that overlap the key register unit.
1551*5f7ddb14SDimitry Andric   DenseMap<unsigned, SmallVector<MIRegs, 2>> SeenDbgInstrs;
15520b57cec5SDimitry Andric 
15530b57cec5SDimitry Andric   /// Sink Copy instructions unused in the same block close to their uses in
15540b57cec5SDimitry Andric   /// successors.
15550b57cec5SDimitry Andric   bool tryToSinkCopy(MachineBasicBlock &BB, MachineFunction &MF,
15560b57cec5SDimitry Andric                      const TargetRegisterInfo *TRI, const TargetInstrInfo *TII);
15570b57cec5SDimitry Andric };
15580b57cec5SDimitry Andric } // namespace
15590b57cec5SDimitry Andric 
15600b57cec5SDimitry Andric char PostRAMachineSinking::ID = 0;
15610b57cec5SDimitry Andric char &llvm::PostRAMachineSinkingID = PostRAMachineSinking::ID;
15620b57cec5SDimitry Andric 
15630b57cec5SDimitry Andric INITIALIZE_PASS(PostRAMachineSinking, "postra-machine-sink",
15640b57cec5SDimitry Andric                 "PostRA Machine Sink", false, false)
15650b57cec5SDimitry Andric 
aliasWithRegsInLiveIn(MachineBasicBlock & MBB,unsigned Reg,const TargetRegisterInfo * TRI)15660b57cec5SDimitry Andric static bool aliasWithRegsInLiveIn(MachineBasicBlock &MBB, unsigned Reg,
15670b57cec5SDimitry Andric                                   const TargetRegisterInfo *TRI) {
15680b57cec5SDimitry Andric   LiveRegUnits LiveInRegUnits(*TRI);
15690b57cec5SDimitry Andric   LiveInRegUnits.addLiveIns(MBB);
15700b57cec5SDimitry Andric   return !LiveInRegUnits.available(Reg);
15710b57cec5SDimitry Andric }
15720b57cec5SDimitry Andric 
15730b57cec5SDimitry Andric static MachineBasicBlock *
getSingleLiveInSuccBB(MachineBasicBlock & CurBB,const SmallPtrSetImpl<MachineBasicBlock * > & SinkableBBs,unsigned Reg,const TargetRegisterInfo * TRI)15740b57cec5SDimitry Andric getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
15750b57cec5SDimitry Andric                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
15760b57cec5SDimitry Andric                       unsigned Reg, const TargetRegisterInfo *TRI) {
15770b57cec5SDimitry Andric   // Try to find a single sinkable successor in which Reg is live-in.
15780b57cec5SDimitry Andric   MachineBasicBlock *BB = nullptr;
15790b57cec5SDimitry Andric   for (auto *SI : SinkableBBs) {
15800b57cec5SDimitry Andric     if (aliasWithRegsInLiveIn(*SI, Reg, TRI)) {
15810b57cec5SDimitry Andric       // If BB is set here, Reg is live-in to at least two sinkable successors,
15820b57cec5SDimitry Andric       // so quit.
15830b57cec5SDimitry Andric       if (BB)
15840b57cec5SDimitry Andric         return nullptr;
15850b57cec5SDimitry Andric       BB = SI;
15860b57cec5SDimitry Andric     }
15870b57cec5SDimitry Andric   }
15880b57cec5SDimitry Andric   // Reg is not live-in to any sinkable successors.
15890b57cec5SDimitry Andric   if (!BB)
15900b57cec5SDimitry Andric     return nullptr;
15910b57cec5SDimitry Andric 
15920b57cec5SDimitry Andric   // Check if any register aliased with Reg is live-in in other successors.
15930b57cec5SDimitry Andric   for (auto *SI : CurBB.successors()) {
15940b57cec5SDimitry Andric     if (!SinkableBBs.count(SI) && aliasWithRegsInLiveIn(*SI, Reg, TRI))
15950b57cec5SDimitry Andric       return nullptr;
15960b57cec5SDimitry Andric   }
15970b57cec5SDimitry Andric   return BB;
15980b57cec5SDimitry Andric }
15990b57cec5SDimitry Andric 
16000b57cec5SDimitry Andric static MachineBasicBlock *
getSingleLiveInSuccBB(MachineBasicBlock & CurBB,const SmallPtrSetImpl<MachineBasicBlock * > & SinkableBBs,ArrayRef<unsigned> DefedRegsInCopy,const TargetRegisterInfo * TRI)16010b57cec5SDimitry Andric getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
16020b57cec5SDimitry Andric                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
16030b57cec5SDimitry Andric                       ArrayRef<unsigned> DefedRegsInCopy,
16040b57cec5SDimitry Andric                       const TargetRegisterInfo *TRI) {
16050b57cec5SDimitry Andric   MachineBasicBlock *SingleBB = nullptr;
16060b57cec5SDimitry Andric   for (auto DefReg : DefedRegsInCopy) {
16070b57cec5SDimitry Andric     MachineBasicBlock *BB =
16080b57cec5SDimitry Andric         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefReg, TRI);
16090b57cec5SDimitry Andric     if (!BB || (SingleBB && SingleBB != BB))
16100b57cec5SDimitry Andric       return nullptr;
16110b57cec5SDimitry Andric     SingleBB = BB;
16120b57cec5SDimitry Andric   }
16130b57cec5SDimitry Andric   return SingleBB;
16140b57cec5SDimitry Andric }
16150b57cec5SDimitry Andric 
clearKillFlags(MachineInstr * MI,MachineBasicBlock & CurBB,SmallVectorImpl<unsigned> & UsedOpsInCopy,LiveRegUnits & UsedRegUnits,const TargetRegisterInfo * TRI)16160b57cec5SDimitry Andric static void clearKillFlags(MachineInstr *MI, MachineBasicBlock &CurBB,
16170b57cec5SDimitry Andric                            SmallVectorImpl<unsigned> &UsedOpsInCopy,
16180b57cec5SDimitry Andric                            LiveRegUnits &UsedRegUnits,
16190b57cec5SDimitry Andric                            const TargetRegisterInfo *TRI) {
16200b57cec5SDimitry Andric   for (auto U : UsedOpsInCopy) {
16210b57cec5SDimitry Andric     MachineOperand &MO = MI->getOperand(U);
16228bcb0991SDimitry Andric     Register SrcReg = MO.getReg();
16230b57cec5SDimitry Andric     if (!UsedRegUnits.available(SrcReg)) {
16240b57cec5SDimitry Andric       MachineBasicBlock::iterator NI = std::next(MI->getIterator());
16250b57cec5SDimitry Andric       for (MachineInstr &UI : make_range(NI, CurBB.end())) {
16260b57cec5SDimitry Andric         if (UI.killsRegister(SrcReg, TRI)) {
16270b57cec5SDimitry Andric           UI.clearRegisterKills(SrcReg, TRI);
16280b57cec5SDimitry Andric           MO.setIsKill(true);
16290b57cec5SDimitry Andric           break;
16300b57cec5SDimitry Andric         }
16310b57cec5SDimitry Andric       }
16320b57cec5SDimitry Andric     }
16330b57cec5SDimitry Andric   }
16340b57cec5SDimitry Andric }
16350b57cec5SDimitry Andric 
updateLiveIn(MachineInstr * MI,MachineBasicBlock * SuccBB,SmallVectorImpl<unsigned> & UsedOpsInCopy,SmallVectorImpl<unsigned> & DefedRegsInCopy)16360b57cec5SDimitry Andric static void updateLiveIn(MachineInstr *MI, MachineBasicBlock *SuccBB,
16370b57cec5SDimitry Andric                          SmallVectorImpl<unsigned> &UsedOpsInCopy,
16380b57cec5SDimitry Andric                          SmallVectorImpl<unsigned> &DefedRegsInCopy) {
16390b57cec5SDimitry Andric   MachineFunction &MF = *SuccBB->getParent();
16400b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
16410b57cec5SDimitry Andric   for (unsigned DefReg : DefedRegsInCopy)
16420b57cec5SDimitry Andric     for (MCSubRegIterator S(DefReg, TRI, true); S.isValid(); ++S)
16430b57cec5SDimitry Andric       SuccBB->removeLiveIn(*S);
16440b57cec5SDimitry Andric   for (auto U : UsedOpsInCopy) {
1645480093f4SDimitry Andric     Register SrcReg = MI->getOperand(U).getReg();
1646480093f4SDimitry Andric     LaneBitmask Mask;
1647480093f4SDimitry Andric     for (MCRegUnitMaskIterator S(SrcReg, TRI); S.isValid(); ++S) {
1648480093f4SDimitry Andric       Mask |= (*S).second;
16490b57cec5SDimitry Andric     }
1650480093f4SDimitry Andric     SuccBB->addLiveIn(SrcReg, Mask.any() ? Mask : LaneBitmask::getAll());
1651480093f4SDimitry Andric   }
1652480093f4SDimitry Andric   SuccBB->sortUniqueLiveIns();
16530b57cec5SDimitry Andric }
16540b57cec5SDimitry Andric 
hasRegisterDependency(MachineInstr * MI,SmallVectorImpl<unsigned> & UsedOpsInCopy,SmallVectorImpl<unsigned> & DefedRegsInCopy,LiveRegUnits & ModifiedRegUnits,LiveRegUnits & UsedRegUnits)16550b57cec5SDimitry Andric static bool hasRegisterDependency(MachineInstr *MI,
16560b57cec5SDimitry Andric                                   SmallVectorImpl<unsigned> &UsedOpsInCopy,
16570b57cec5SDimitry Andric                                   SmallVectorImpl<unsigned> &DefedRegsInCopy,
16580b57cec5SDimitry Andric                                   LiveRegUnits &ModifiedRegUnits,
16590b57cec5SDimitry Andric                                   LiveRegUnits &UsedRegUnits) {
16600b57cec5SDimitry Andric   bool HasRegDependency = false;
16610b57cec5SDimitry Andric   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
16620b57cec5SDimitry Andric     MachineOperand &MO = MI->getOperand(i);
16630b57cec5SDimitry Andric     if (!MO.isReg())
16640b57cec5SDimitry Andric       continue;
16658bcb0991SDimitry Andric     Register Reg = MO.getReg();
16660b57cec5SDimitry Andric     if (!Reg)
16670b57cec5SDimitry Andric       continue;
16680b57cec5SDimitry Andric     if (MO.isDef()) {
16690b57cec5SDimitry Andric       if (!ModifiedRegUnits.available(Reg) || !UsedRegUnits.available(Reg)) {
16700b57cec5SDimitry Andric         HasRegDependency = true;
16710b57cec5SDimitry Andric         break;
16720b57cec5SDimitry Andric       }
16730b57cec5SDimitry Andric       DefedRegsInCopy.push_back(Reg);
16740b57cec5SDimitry Andric 
16750b57cec5SDimitry Andric       // FIXME: instead of isUse(), readsReg() would be a better fix here,
16760b57cec5SDimitry Andric       // For example, we can ignore modifications in reg with undef. However,
16770b57cec5SDimitry Andric       // it's not perfectly clear if skipping the internal read is safe in all
16780b57cec5SDimitry Andric       // other targets.
16790b57cec5SDimitry Andric     } else if (MO.isUse()) {
16800b57cec5SDimitry Andric       if (!ModifiedRegUnits.available(Reg)) {
16810b57cec5SDimitry Andric         HasRegDependency = true;
16820b57cec5SDimitry Andric         break;
16830b57cec5SDimitry Andric       }
16840b57cec5SDimitry Andric       UsedOpsInCopy.push_back(i);
16850b57cec5SDimitry Andric     }
16860b57cec5SDimitry Andric   }
16870b57cec5SDimitry Andric   return HasRegDependency;
16880b57cec5SDimitry Andric }
16890b57cec5SDimitry Andric 
getRegUnits(MCRegister Reg,const TargetRegisterInfo * TRI)1690af732203SDimitry Andric static SmallSet<MCRegister, 4> getRegUnits(MCRegister Reg,
16918bcb0991SDimitry Andric                                            const TargetRegisterInfo *TRI) {
1692af732203SDimitry Andric   SmallSet<MCRegister, 4> RegUnits;
16938bcb0991SDimitry Andric   for (auto RI = MCRegUnitIterator(Reg, TRI); RI.isValid(); ++RI)
16948bcb0991SDimitry Andric     RegUnits.insert(*RI);
16958bcb0991SDimitry Andric   return RegUnits;
16968bcb0991SDimitry Andric }
16978bcb0991SDimitry Andric 
tryToSinkCopy(MachineBasicBlock & CurBB,MachineFunction & MF,const TargetRegisterInfo * TRI,const TargetInstrInfo * TII)16980b57cec5SDimitry Andric bool PostRAMachineSinking::tryToSinkCopy(MachineBasicBlock &CurBB,
16990b57cec5SDimitry Andric                                          MachineFunction &MF,
17000b57cec5SDimitry Andric                                          const TargetRegisterInfo *TRI,
17010b57cec5SDimitry Andric                                          const TargetInstrInfo *TII) {
17020b57cec5SDimitry Andric   SmallPtrSet<MachineBasicBlock *, 2> SinkableBBs;
17030b57cec5SDimitry Andric   // FIXME: For now, we sink only to a successor which has a single predecessor
17040b57cec5SDimitry Andric   // so that we can directly sink COPY instructions to the successor without
17050b57cec5SDimitry Andric   // adding any new block or branch instruction.
17060b57cec5SDimitry Andric   for (MachineBasicBlock *SI : CurBB.successors())
17070b57cec5SDimitry Andric     if (!SI->livein_empty() && SI->pred_size() == 1)
17080b57cec5SDimitry Andric       SinkableBBs.insert(SI);
17090b57cec5SDimitry Andric 
17100b57cec5SDimitry Andric   if (SinkableBBs.empty())
17110b57cec5SDimitry Andric     return false;
17120b57cec5SDimitry Andric 
17130b57cec5SDimitry Andric   bool Changed = false;
17140b57cec5SDimitry Andric 
17150b57cec5SDimitry Andric   // Track which registers have been modified and used between the end of the
17160b57cec5SDimitry Andric   // block and the current instruction.
17170b57cec5SDimitry Andric   ModifiedRegUnits.clear();
17180b57cec5SDimitry Andric   UsedRegUnits.clear();
17190b57cec5SDimitry Andric   SeenDbgInstrs.clear();
17200b57cec5SDimitry Andric 
17210b57cec5SDimitry Andric   for (auto I = CurBB.rbegin(), E = CurBB.rend(); I != E;) {
17220b57cec5SDimitry Andric     MachineInstr *MI = &*I;
17230b57cec5SDimitry Andric     ++I;
17240b57cec5SDimitry Andric 
17250b57cec5SDimitry Andric     // Track the operand index for use in Copy.
17260b57cec5SDimitry Andric     SmallVector<unsigned, 2> UsedOpsInCopy;
17270b57cec5SDimitry Andric     // Track the register number defed in Copy.
17280b57cec5SDimitry Andric     SmallVector<unsigned, 2> DefedRegsInCopy;
17290b57cec5SDimitry Andric 
17300b57cec5SDimitry Andric     // We must sink this DBG_VALUE if its operand is sunk. To avoid searching
17310b57cec5SDimitry Andric     // for DBG_VALUEs later, record them when they're encountered.
17320b57cec5SDimitry Andric     if (MI->isDebugValue()) {
1733*5f7ddb14SDimitry Andric       SmallDenseMap<MCRegister, SmallVector<unsigned, 2>, 4> MIUnits;
1734*5f7ddb14SDimitry Andric       bool IsValid = true;
1735*5f7ddb14SDimitry Andric       for (MachineOperand &MO : MI->debug_operands()) {
17368bcb0991SDimitry Andric         if (MO.isReg() && Register::isPhysicalRegister(MO.getReg())) {
17370b57cec5SDimitry Andric           // Bail if we can already tell the sink would be rejected, rather
17380b57cec5SDimitry Andric           // than needlessly accumulating lots of DBG_VALUEs.
17390b57cec5SDimitry Andric           if (hasRegisterDependency(MI, UsedOpsInCopy, DefedRegsInCopy,
1740*5f7ddb14SDimitry Andric                                     ModifiedRegUnits, UsedRegUnits)) {
1741*5f7ddb14SDimitry Andric             IsValid = false;
1742*5f7ddb14SDimitry Andric             break;
1743*5f7ddb14SDimitry Andric           }
17440b57cec5SDimitry Andric 
17458bcb0991SDimitry Andric           // Record debug use of each reg unit.
1746*5f7ddb14SDimitry Andric           SmallSet<MCRegister, 4> RegUnits = getRegUnits(MO.getReg(), TRI);
1747*5f7ddb14SDimitry Andric           for (MCRegister Reg : RegUnits)
1748*5f7ddb14SDimitry Andric             MIUnits[Reg].push_back(MO.getReg());
1749*5f7ddb14SDimitry Andric         }
1750*5f7ddb14SDimitry Andric       }
1751*5f7ddb14SDimitry Andric       if (IsValid) {
1752*5f7ddb14SDimitry Andric         for (auto RegOps : MIUnits)
1753*5f7ddb14SDimitry Andric           SeenDbgInstrs[RegOps.first].push_back({MI, RegOps.second});
17540b57cec5SDimitry Andric       }
17550b57cec5SDimitry Andric       continue;
17560b57cec5SDimitry Andric     }
17570b57cec5SDimitry Andric 
1758*5f7ddb14SDimitry Andric     if (MI->isDebugOrPseudoInstr())
17590b57cec5SDimitry Andric       continue;
17600b57cec5SDimitry Andric 
17610b57cec5SDimitry Andric     // Do not move any instruction across function call.
17620b57cec5SDimitry Andric     if (MI->isCall())
17630b57cec5SDimitry Andric       return false;
17640b57cec5SDimitry Andric 
17650b57cec5SDimitry Andric     if (!MI->isCopy() || !MI->getOperand(0).isRenamable()) {
17660b57cec5SDimitry Andric       LiveRegUnits::accumulateUsedDefed(*MI, ModifiedRegUnits, UsedRegUnits,
17670b57cec5SDimitry Andric                                         TRI);
17680b57cec5SDimitry Andric       continue;
17690b57cec5SDimitry Andric     }
17700b57cec5SDimitry Andric 
17710b57cec5SDimitry Andric     // Don't sink the COPY if it would violate a register dependency.
17720b57cec5SDimitry Andric     if (hasRegisterDependency(MI, UsedOpsInCopy, DefedRegsInCopy,
17730b57cec5SDimitry Andric                               ModifiedRegUnits, UsedRegUnits)) {
17740b57cec5SDimitry Andric       LiveRegUnits::accumulateUsedDefed(*MI, ModifiedRegUnits, UsedRegUnits,
17750b57cec5SDimitry Andric                                         TRI);
17760b57cec5SDimitry Andric       continue;
17770b57cec5SDimitry Andric     }
17780b57cec5SDimitry Andric     assert((!UsedOpsInCopy.empty() && !DefedRegsInCopy.empty()) &&
17790b57cec5SDimitry Andric            "Unexpect SrcReg or DefReg");
17800b57cec5SDimitry Andric     MachineBasicBlock *SuccBB =
17810b57cec5SDimitry Andric         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefedRegsInCopy, TRI);
17820b57cec5SDimitry Andric     // Don't sink if we cannot find a single sinkable successor in which Reg
17830b57cec5SDimitry Andric     // is live-in.
17840b57cec5SDimitry Andric     if (!SuccBB) {
17850b57cec5SDimitry Andric       LiveRegUnits::accumulateUsedDefed(*MI, ModifiedRegUnits, UsedRegUnits,
17860b57cec5SDimitry Andric                                         TRI);
17870b57cec5SDimitry Andric       continue;
17880b57cec5SDimitry Andric     }
17890b57cec5SDimitry Andric     assert((SuccBB->pred_size() == 1 && *SuccBB->pred_begin() == &CurBB) &&
17900b57cec5SDimitry Andric            "Unexpected predecessor");
17910b57cec5SDimitry Andric 
17928bcb0991SDimitry Andric     // Collect DBG_VALUEs that must sink with this copy. We've previously
17938bcb0991SDimitry Andric     // recorded which reg units that DBG_VALUEs read, if this instruction
17948bcb0991SDimitry Andric     // writes any of those units then the corresponding DBG_VALUEs must sink.
1795*5f7ddb14SDimitry Andric     MapVector<MachineInstr *, MIRegs::second_type> DbgValsToSinkMap;
17960b57cec5SDimitry Andric     for (auto &MO : MI->operands()) {
17970b57cec5SDimitry Andric       if (!MO.isReg() || !MO.isDef())
17980b57cec5SDimitry Andric         continue;
17998bcb0991SDimitry Andric 
1800af732203SDimitry Andric       SmallSet<MCRegister, 4> Units = getRegUnits(MO.getReg(), TRI);
1801*5f7ddb14SDimitry Andric       for (MCRegister Reg : Units) {
1802*5f7ddb14SDimitry Andric         for (auto MIRegs : SeenDbgInstrs.lookup(Reg)) {
1803*5f7ddb14SDimitry Andric           auto &Regs = DbgValsToSinkMap[MIRegs.first];
1804*5f7ddb14SDimitry Andric           for (unsigned Reg : MIRegs.second)
1805*5f7ddb14SDimitry Andric             Regs.push_back(Reg);
18060b57cec5SDimitry Andric         }
1807*5f7ddb14SDimitry Andric       }
1808*5f7ddb14SDimitry Andric     }
1809*5f7ddb14SDimitry Andric     SmallVector<MIRegs, 4> DbgValsToSink(DbgValsToSinkMap.begin(),
1810*5f7ddb14SDimitry Andric                                          DbgValsToSinkMap.end());
18110b57cec5SDimitry Andric 
18120b57cec5SDimitry Andric     // Clear the kill flag if SrcReg is killed between MI and the end of the
18130b57cec5SDimitry Andric     // block.
18140b57cec5SDimitry Andric     clearKillFlags(MI, CurBB, UsedOpsInCopy, UsedRegUnits, TRI);
18150b57cec5SDimitry Andric     MachineBasicBlock::iterator InsertPos = SuccBB->getFirstNonPHI();
1816480093f4SDimitry Andric     performSink(*MI, *SuccBB, InsertPos, DbgValsToSink);
18170b57cec5SDimitry Andric     updateLiveIn(MI, SuccBB, UsedOpsInCopy, DefedRegsInCopy);
18180b57cec5SDimitry Andric 
18190b57cec5SDimitry Andric     Changed = true;
18200b57cec5SDimitry Andric     ++NumPostRACopySink;
18210b57cec5SDimitry Andric   }
18220b57cec5SDimitry Andric   return Changed;
18230b57cec5SDimitry Andric }
18240b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)18250b57cec5SDimitry Andric bool PostRAMachineSinking::runOnMachineFunction(MachineFunction &MF) {
18260b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
18270b57cec5SDimitry Andric     return false;
18280b57cec5SDimitry Andric 
18290b57cec5SDimitry Andric   bool Changed = false;
18300b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
18310b57cec5SDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
18320b57cec5SDimitry Andric 
18330b57cec5SDimitry Andric   ModifiedRegUnits.init(*TRI);
18340b57cec5SDimitry Andric   UsedRegUnits.init(*TRI);
18350b57cec5SDimitry Andric   for (auto &BB : MF)
18360b57cec5SDimitry Andric     Changed |= tryToSinkCopy(BB, MF, TRI, TII);
18370b57cec5SDimitry Andric 
18380b57cec5SDimitry Andric   return Changed;
18390b57cec5SDimitry Andric }
1840