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