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