SCCP.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
  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 file implements sparse conditional constant propagation and merging:
  10. //
  11. // Specifically, this:
  12. // * Assumes values are constant unless proven otherwise
  13. // * Assumes BasicBlocks are dead unless proven otherwise
  14. // * Proves values to be constant, and replaces them with constants
  15. // * Proves conditional branches to be unconditional
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/Transforms/Scalar/SCCP.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/MapVector.h"
  21. #include "llvm/ADT/STLExtras.h"
  22. #include "llvm/ADT/SetVector.h"
  23. #include "llvm/ADT/SmallPtrSet.h"
  24. #include "llvm/ADT/SmallVector.h"
  25. #include "llvm/ADT/Statistic.h"
  26. #include "llvm/Analysis/DomTreeUpdater.h"
  27. #include "llvm/Analysis/GlobalsModRef.h"
  28. #include "llvm/Analysis/TargetLibraryInfo.h"
  29. #include "llvm/Analysis/ValueTracking.h"
  30. #include "llvm/IR/BasicBlock.h"
  31. #include "llvm/IR/Constant.h"
  32. #include "llvm/IR/DerivedTypes.h"
  33. #include "llvm/IR/Function.h"
  34. #include "llvm/IR/GlobalVariable.h"
  35. #include "llvm/IR/InstrTypes.h"
  36. #include "llvm/IR/Instruction.h"
  37. #include "llvm/IR/Instructions.h"
  38. #include "llvm/IR/Module.h"
  39. #include "llvm/IR/PassManager.h"
  40. #include "llvm/IR/Type.h"
  41. #include "llvm/IR/User.h"
  42. #include "llvm/IR/Value.h"
  43. #include "llvm/InitializePasses.h"
  44. #include "llvm/Pass.h"
  45. #include "llvm/Support/Casting.h"
  46. #include "llvm/Support/Debug.h"
  47. #include "llvm/Support/ErrorHandling.h"
  48. #include "llvm/Support/raw_ostream.h"
  49. #include "llvm/Transforms/Scalar.h"
  50. #include "llvm/Transforms/Utils/Local.h"
  51. #include "llvm/Transforms/Utils/SCCPSolver.h"
  52. #include <cassert>
  53. #include <utility>
  54. #include <vector>
  55. using namespace llvm;
  56. #define DEBUG_TYPE "sccp"
  57. STATISTIC(NumInstRemoved, "Number of instructions removed");
  58. STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
  59. STATISTIC(NumInstReplaced,
  60. "Number of instructions replaced with (simpler) instruction");
  61. // runSCCP() - Run the Sparse Conditional Constant Propagation algorithm,
  62. // and return true if the function was modified.
  63. static bool runSCCP(Function &F, const DataLayout &DL,
  64. const TargetLibraryInfo *TLI, DomTreeUpdater &DTU) {
  65. LLVM_DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
  66. SCCPSolver Solver(
  67. DL, [TLI](Function &F) -> const TargetLibraryInfo & { return *TLI; },
  68. F.getContext());
  69. // Mark the first block of the function as being executable.
  70. Solver.markBlockExecutable(&F.front());
  71. // Mark all arguments to the function as being overdefined.
  72. for (Argument &AI : F.args())
  73. Solver.markOverdefined(&AI);
  74. // Solve for constants.
  75. bool ResolvedUndefs = true;
  76. while (ResolvedUndefs) {
  77. Solver.solve();
  78. LLVM_DEBUG(dbgs() << "RESOLVING UNDEFs\n");
  79. ResolvedUndefs = Solver.resolvedUndefsIn(F);
  80. }
  81. bool MadeChanges = false;
  82. // If we decided that there are basic blocks that are dead in this function,
  83. // delete their contents now. Note that we cannot actually delete the blocks,
  84. // as we cannot modify the CFG of the function.
  85. SmallPtrSet<Value *, 32> InsertedValues;
  86. SmallVector<BasicBlock *, 8> BlocksToErase;
  87. for (BasicBlock &BB : F) {
  88. if (!Solver.isBlockExecutable(&BB)) {
  89. LLVM_DEBUG(dbgs() << " BasicBlock Dead:" << BB);
  90. ++NumDeadBlocks;
  91. BlocksToErase.push_back(&BB);
  92. MadeChanges = true;
  93. continue;
  94. }
  95. MadeChanges |= Solver.simplifyInstsInBlock(BB, InsertedValues,
  96. NumInstRemoved, NumInstReplaced);
  97. }
  98. // Remove unreachable blocks and non-feasible edges.
  99. for (BasicBlock *DeadBB : BlocksToErase)
  100. NumInstRemoved += changeToUnreachable(DeadBB->getFirstNonPHI(),
  101. /*PreserveLCSSA=*/false, &DTU);
  102. BasicBlock *NewUnreachableBB = nullptr;
  103. for (BasicBlock &BB : F)
  104. MadeChanges |= Solver.removeNonFeasibleEdges(&BB, DTU, NewUnreachableBB);
  105. for (BasicBlock *DeadBB : BlocksToErase)
  106. if (!DeadBB->hasAddressTaken())
  107. DTU.deleteBB(DeadBB);
  108. return MadeChanges;
  109. }
  110. PreservedAnalyses SCCPPass::run(Function &F, FunctionAnalysisManager &AM) {
  111. const DataLayout &DL = F.getParent()->getDataLayout();
  112. auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
  113. auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
  114. DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
  115. if (!runSCCP(F, DL, &TLI, DTU))
  116. return PreservedAnalyses::all();
  117. auto PA = PreservedAnalyses();
  118. PA.preserve<DominatorTreeAnalysis>();
  119. return PA;
  120. }
  121. namespace {
  122. //===--------------------------------------------------------------------===//
  123. //
  124. /// SCCP Class - This class uses the SCCPSolver to implement a per-function
  125. /// Sparse Conditional Constant Propagator.
  126. ///
  127. class SCCPLegacyPass : public FunctionPass {
  128. public:
  129. // Pass identification, replacement for typeid
  130. static char ID;
  131. SCCPLegacyPass() : FunctionPass(ID) {
  132. initializeSCCPLegacyPassPass(*PassRegistry::getPassRegistry());
  133. }
  134. void getAnalysisUsage(AnalysisUsage &AU) const override {
  135. AU.addRequired<TargetLibraryInfoWrapperPass>();
  136. AU.addPreserved<GlobalsAAWrapperPass>();
  137. AU.addPreserved<DominatorTreeWrapperPass>();
  138. }
  139. // runOnFunction - Run the Sparse Conditional Constant Propagation
  140. // algorithm, and return true if the function was modified.
  141. bool runOnFunction(Function &F) override {
  142. if (skipFunction(F))
  143. return false;
  144. const DataLayout &DL = F.getParent()->getDataLayout();
  145. const TargetLibraryInfo *TLI =
  146. &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  147. auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  148. DomTreeUpdater DTU(DTWP ? &DTWP->getDomTree() : nullptr,
  149. DomTreeUpdater::UpdateStrategy::Lazy);
  150. return runSCCP(F, DL, TLI, DTU);
  151. }
  152. };
  153. } // end anonymous namespace
  154. char SCCPLegacyPass::ID = 0;
  155. INITIALIZE_PASS_BEGIN(SCCPLegacyPass, "sccp",
  156. "Sparse Conditional Constant Propagation", false, false)
  157. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  158. INITIALIZE_PASS_END(SCCPLegacyPass, "sccp",
  159. "Sparse Conditional Constant Propagation", false, false)
  160. // createSCCPPass - This is the public interface to this file.
  161. FunctionPass *llvm::createSCCPPass() { return new SCCPLegacyPass(); }