Sink.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //===-- Sink.cpp - Code Sinking -------------------------------------------===//
  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. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Scalar/Sink.h"
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/Analysis/AliasAnalysis.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/Analysis/ValueTracking.h"
  18. #include "llvm/IR/CFG.h"
  19. #include "llvm/IR/DataLayout.h"
  20. #include "llvm/IR/Dominators.h"
  21. #include "llvm/IR/IntrinsicInst.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/InitializePasses.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include "llvm/Transforms/Scalar.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "sink"
  29. STATISTIC(NumSunk, "Number of instructions sunk");
  30. STATISTIC(NumSinkIter, "Number of sinking iterations");
  31. static bool isSafeToMove(Instruction *Inst, AliasAnalysis &AA,
  32. SmallPtrSetImpl<Instruction *> &Stores) {
  33. if (Inst->mayWriteToMemory()) {
  34. Stores.insert(Inst);
  35. return false;
  36. }
  37. if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
  38. MemoryLocation Loc = MemoryLocation::get(L);
  39. for (Instruction *S : Stores)
  40. if (isModSet(AA.getModRefInfo(S, Loc)))
  41. return false;
  42. }
  43. if (Inst->isTerminator() || isa<PHINode>(Inst) || Inst->isEHPad() ||
  44. Inst->mayThrow())
  45. return false;
  46. if (auto *Call = dyn_cast<CallBase>(Inst)) {
  47. // Convergent operations cannot be made control-dependent on additional
  48. // values.
  49. if (Call->isConvergent())
  50. return false;
  51. for (Instruction *S : Stores)
  52. if (isModSet(AA.getModRefInfo(S, Call)))
  53. return false;
  54. }
  55. return true;
  56. }
  57. /// IsAcceptableTarget - Return true if it is possible to sink the instruction
  58. /// in the specified basic block.
  59. static bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo,
  60. DominatorTree &DT, LoopInfo &LI) {
  61. assert(Inst && "Instruction to be sunk is null");
  62. assert(SuccToSinkTo && "Candidate sink target is null");
  63. // It's never legal to sink an instruction into a block which terminates in an
  64. // EH-pad.
  65. if (SuccToSinkTo->getTerminator()->isExceptionalTerminator())
  66. return false;
  67. // If the block has multiple predecessors, this would introduce computation
  68. // on different code paths. We could split the critical edge, but for now we
  69. // just punt.
  70. // FIXME: Split critical edges if not backedges.
  71. if (SuccToSinkTo->getUniquePredecessor() != Inst->getParent()) {
  72. // We cannot sink a load across a critical edge - there may be stores in
  73. // other code paths.
  74. if (Inst->mayReadFromMemory())
  75. return false;
  76. // We don't want to sink across a critical edge if we don't dominate the
  77. // successor. We could be introducing calculations to new code paths.
  78. if (!DT.dominates(Inst->getParent(), SuccToSinkTo))
  79. return false;
  80. // Don't sink instructions into a loop.
  81. Loop *succ = LI.getLoopFor(SuccToSinkTo);
  82. Loop *cur = LI.getLoopFor(Inst->getParent());
  83. if (succ != nullptr && succ != cur)
  84. return false;
  85. }
  86. return true;
  87. }
  88. /// SinkInstruction - Determine whether it is safe to sink the specified machine
  89. /// instruction out of its current block into a successor.
  90. static bool SinkInstruction(Instruction *Inst,
  91. SmallPtrSetImpl<Instruction *> &Stores,
  92. DominatorTree &DT, LoopInfo &LI, AAResults &AA) {
  93. // Don't sink static alloca instructions. CodeGen assumes allocas outside the
  94. // entry block are dynamically sized stack objects.
  95. if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
  96. if (AI->isStaticAlloca())
  97. return false;
  98. // Check if it's safe to move the instruction.
  99. if (!isSafeToMove(Inst, AA, Stores))
  100. return false;
  101. // FIXME: This should include support for sinking instructions within the
  102. // block they are currently in to shorten the live ranges. We often get
  103. // instructions sunk into the top of a large block, but it would be better to
  104. // also sink them down before their first use in the block. This xform has to
  105. // be careful not to *increase* register pressure though, e.g. sinking
  106. // "x = y + z" down if it kills y and z would increase the live ranges of y
  107. // and z and only shrink the live range of x.
  108. // SuccToSinkTo - This is the successor to sink this instruction to, once we
  109. // decide.
  110. BasicBlock *SuccToSinkTo = nullptr;
  111. // Find the nearest common dominator of all users as the candidate.
  112. BasicBlock *BB = Inst->getParent();
  113. for (Use &U : Inst->uses()) {
  114. Instruction *UseInst = cast<Instruction>(U.getUser());
  115. BasicBlock *UseBlock = UseInst->getParent();
  116. // Don't worry about dead users.
  117. if (!DT.isReachableFromEntry(UseBlock))
  118. continue;
  119. if (PHINode *PN = dyn_cast<PHINode>(UseInst)) {
  120. // PHI nodes use the operand in the predecessor block, not the block with
  121. // the PHI.
  122. unsigned Num = PHINode::getIncomingValueNumForOperand(U.getOperandNo());
  123. UseBlock = PN->getIncomingBlock(Num);
  124. }
  125. if (SuccToSinkTo)
  126. SuccToSinkTo = DT.findNearestCommonDominator(SuccToSinkTo, UseBlock);
  127. else
  128. SuccToSinkTo = UseBlock;
  129. // The current basic block needs to dominate the candidate.
  130. if (!DT.dominates(BB, SuccToSinkTo))
  131. return false;
  132. }
  133. if (SuccToSinkTo) {
  134. // The nearest common dominator may be in a parent loop of BB, which may not
  135. // be beneficial. Find an ancestor.
  136. while (SuccToSinkTo != BB &&
  137. !IsAcceptableTarget(Inst, SuccToSinkTo, DT, LI))
  138. SuccToSinkTo = DT.getNode(SuccToSinkTo)->getIDom()->getBlock();
  139. if (SuccToSinkTo == BB)
  140. SuccToSinkTo = nullptr;
  141. }
  142. // If we couldn't find a block to sink to, ignore this instruction.
  143. if (!SuccToSinkTo)
  144. return false;
  145. LLVM_DEBUG(dbgs() << "Sink" << *Inst << " (";
  146. Inst->getParent()->printAsOperand(dbgs(), false); dbgs() << " -> ";
  147. SuccToSinkTo->printAsOperand(dbgs(), false); dbgs() << ")\n");
  148. // Move the instruction.
  149. Inst->moveBefore(&*SuccToSinkTo->getFirstInsertionPt());
  150. return true;
  151. }
  152. static bool ProcessBlock(BasicBlock &BB, DominatorTree &DT, LoopInfo &LI,
  153. AAResults &AA) {
  154. // Can't sink anything out of a block that has less than two successors.
  155. if (BB.getTerminator()->getNumSuccessors() <= 1) return false;
  156. // Don't bother sinking code out of unreachable blocks. In addition to being
  157. // unprofitable, it can also lead to infinite looping, because in an
  158. // unreachable loop there may be nowhere to stop.
  159. if (!DT.isReachableFromEntry(&BB)) return false;
  160. bool MadeChange = false;
  161. // Walk the basic block bottom-up. Remember if we saw a store.
  162. BasicBlock::iterator I = BB.end();
  163. --I;
  164. bool ProcessedBegin = false;
  165. SmallPtrSet<Instruction *, 8> Stores;
  166. do {
  167. Instruction *Inst = &*I; // The instruction to sink.
  168. // Predecrement I (if it's not begin) so that it isn't invalidated by
  169. // sinking.
  170. ProcessedBegin = I == BB.begin();
  171. if (!ProcessedBegin)
  172. --I;
  173. if (Inst->isDebugOrPseudoInst())
  174. continue;
  175. if (SinkInstruction(Inst, Stores, DT, LI, AA)) {
  176. ++NumSunk;
  177. MadeChange = true;
  178. }
  179. // If we just processed the first instruction in the block, we're done.
  180. } while (!ProcessedBegin);
  181. return MadeChange;
  182. }
  183. static bool iterativelySinkInstructions(Function &F, DominatorTree &DT,
  184. LoopInfo &LI, AAResults &AA) {
  185. bool MadeChange, EverMadeChange = false;
  186. do {
  187. MadeChange = false;
  188. LLVM_DEBUG(dbgs() << "Sinking iteration " << NumSinkIter << "\n");
  189. // Process all basic blocks.
  190. for (BasicBlock &I : F)
  191. MadeChange |= ProcessBlock(I, DT, LI, AA);
  192. EverMadeChange |= MadeChange;
  193. NumSinkIter++;
  194. } while (MadeChange);
  195. return EverMadeChange;
  196. }
  197. PreservedAnalyses SinkingPass::run(Function &F, FunctionAnalysisManager &AM) {
  198. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  199. auto &LI = AM.getResult<LoopAnalysis>(F);
  200. auto &AA = AM.getResult<AAManager>(F);
  201. if (!iterativelySinkInstructions(F, DT, LI, AA))
  202. return PreservedAnalyses::all();
  203. PreservedAnalyses PA;
  204. PA.preserveSet<CFGAnalyses>();
  205. return PA;
  206. }
  207. namespace {
  208. class SinkingLegacyPass : public FunctionPass {
  209. public:
  210. static char ID; // Pass identification
  211. SinkingLegacyPass() : FunctionPass(ID) {
  212. initializeSinkingLegacyPassPass(*PassRegistry::getPassRegistry());
  213. }
  214. bool runOnFunction(Function &F) override {
  215. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  216. auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  217. auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
  218. return iterativelySinkInstructions(F, DT, LI, AA);
  219. }
  220. void getAnalysisUsage(AnalysisUsage &AU) const override {
  221. AU.setPreservesCFG();
  222. FunctionPass::getAnalysisUsage(AU);
  223. AU.addRequired<AAResultsWrapperPass>();
  224. AU.addRequired<DominatorTreeWrapperPass>();
  225. AU.addRequired<LoopInfoWrapperPass>();
  226. AU.addPreserved<DominatorTreeWrapperPass>();
  227. AU.addPreserved<LoopInfoWrapperPass>();
  228. }
  229. };
  230. } // end anonymous namespace
  231. char SinkingLegacyPass::ID = 0;
  232. INITIALIZE_PASS_BEGIN(SinkingLegacyPass, "sink", "Code sinking", false, false)
  233. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  234. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  235. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  236. INITIALIZE_PASS_END(SinkingLegacyPass, "sink", "Code sinking", false, false)
  237. FunctionPass *llvm::createSinkingPass() { return new SinkingLegacyPass(); }