MachineSink.cpp 60 KB

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