1 //===-- SCCP.cpp ----------------------------------------------------------===//
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 Interprocedural Sparse Conditional Constant Propagation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Transforms/IPO/SCCP.h"
14 #include "llvm/Analysis/AssumptionCache.h"
15 #include "llvm/Analysis/PostDominators.h"
16 #include "llvm/Analysis/TargetLibraryInfo.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/Transforms/Scalar/SCCP.h"
20 
21 using namespace llvm;
22 
23 PreservedAnalyses IPSCCPPass::run(Module &M, ModuleAnalysisManager &AM) {
24   const DataLayout &DL = M.getDataLayout();
25   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
26   auto GetTLI = [&FAM](Function &F) -> const TargetLibraryInfo & {
27     return FAM.getResult<TargetLibraryAnalysis>(F);
28   };
29   auto getAnalysis = [&FAM](Function &F) -> AnalysisResultsForFn {
30     DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
31     return {
32         std::make_unique<PredicateInfo>(F, DT, FAM.getResult<AssumptionAnalysis>(F)),
33         &DT, FAM.getCachedResult<PostDominatorTreeAnalysis>(F)};
34   };
35 
36   if (!runIPSCCP(M, DL, GetTLI, getAnalysis))
37     return PreservedAnalyses::all();
38 
39   PreservedAnalyses PA;
40   PA.preserve<DominatorTreeAnalysis>();
41   PA.preserve<PostDominatorTreeAnalysis>();
42   PA.preserve<FunctionAnalysisManagerModuleProxy>();
43   return PA;
44 }
45 
46 namespace {
47 
48 //===--------------------------------------------------------------------===//
49 //
50 /// IPSCCP Class - This class implements interprocedural Sparse Conditional
51 /// Constant Propagation.
52 ///
53 class IPSCCPLegacyPass : public ModulePass {
54 public:
55   static char ID;
56 
57   IPSCCPLegacyPass() : ModulePass(ID) {
58     initializeIPSCCPLegacyPassPass(*PassRegistry::getPassRegistry());
59   }
60 
61   bool runOnModule(Module &M) override {
62     if (skipModule(M))
63       return false;
64     const DataLayout &DL = M.getDataLayout();
65     auto GetTLI = [this](Function &F) -> const TargetLibraryInfo & {
66       return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
67     };
68     auto getAnalysis = [this](Function &F) -> AnalysisResultsForFn {
69       DominatorTree &DT =
70           this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
71       return {
72           std::make_unique<PredicateInfo>(
73               F, DT,
74               this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
75                   F)),
76           nullptr,  // We cannot preserve the DT or PDT with the legacy pass
77           nullptr}; // manager, so set them to nullptr.
78     };
79 
80     return runIPSCCP(M, DL, GetTLI, getAnalysis);
81   }
82 
83   void getAnalysisUsage(AnalysisUsage &AU) const override {
84     AU.addRequired<AssumptionCacheTracker>();
85     AU.addRequired<DominatorTreeWrapperPass>();
86     AU.addRequired<TargetLibraryInfoWrapperPass>();
87   }
88 };
89 
90 } // end anonymous namespace
91 
92 char IPSCCPLegacyPass::ID = 0;
93 
94 INITIALIZE_PASS_BEGIN(IPSCCPLegacyPass, "ipsccp",
95                       "Interprocedural Sparse Conditional Constant Propagation",
96                       false, false)
97 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
98 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
99 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
100 INITIALIZE_PASS_END(IPSCCPLegacyPass, "ipsccp",
101                     "Interprocedural Sparse Conditional Constant Propagation",
102                     false, false)
103 
104 // createIPSCCPPass - This is the public interface to this file.
105 ModulePass *llvm::createIPSCCPPass() { return new IPSCCPLegacyPass(); }
106