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