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