Index: include/polly/CodeGen/CodegenCleanup.h =================================================================== --- /dev/null +++ include/polly/CodeGen/CodegenCleanup.h @@ -0,0 +1,41 @@ +#ifndef POLLY_CODEGENCLEANUP_H +#define POLLY_CODEGENCLEANUP_H + +#include "llvm/Pass.h" + +namespace polly { + +class RememberCodegen : public llvm::FunctionPass { +private: + RememberCodegen(const RememberCodegen &) = delete; + const RememberCodegen &operator=(const RememberCodegen &) = delete; + + bool Changed; + +public: + static char ID; + explicit RememberCodegen() : llvm::FunctionPass(ID), Changed(false) {} + + bool hasChanged() const { return Changed; } + void setChanged() { Changed = true; } + + /// @name FunctionPass interface + //@{ + virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override; + virtual void releaseMemory() override {} + virtual bool runOnFunction(llvm::Function &F) override { return false; } + //@} +}; + +llvm::FunctionPass *createRememberCodegenPass(); +llvm::FunctionPass *createCodegenCleanupPass(); +// void registerCodegenCleanupPasses(llvm::legacy::PassManagerBase &PM); +}; + +namespace llvm { +class PassRegistry; +void initializeRememberCodegenPass(llvm::PassRegistry &); +void initializeCodegenCleanupPass(llvm::PassRegistry &); +} + +#endif Index: lib/CMakeLists.txt =================================================================== --- lib/CMakeLists.txt +++ lib/CMakeLists.txt @@ -37,6 +37,7 @@ CodeGen/IRBuilder.cpp CodeGen/Utils.cpp CodeGen/RuntimeDebugBuilder.cpp + CodeGen/CodegenCleanup.cpp ${GPGPU_CODEGEN_FILES} Exchange/JSONExporter.cpp Support/GICHelper.cpp Index: lib/CodeGen/CodeGeneration.cpp =================================================================== --- lib/CodeGen/CodeGeneration.cpp +++ lib/CodeGen/CodeGeneration.cpp @@ -20,6 +20,7 @@ //===----------------------------------------------------------------------===// #include "polly/CodeGen/IslNodeBuilder.h" +#include "polly/CodeGen/CodegenCleanup.h" #include "polly/CodeGen/IslAst.h" #include "polly/CodeGen/Utils.h" #include "polly/DependenceInfo.h" @@ -162,6 +163,10 @@ assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) && "Verification of generated function failed"); + + auto CGR = getAnalysisIfAvailable(); + if (CGR) + CGR->setChanged(); return true; } @@ -174,6 +179,7 @@ AU.addRequired(); AU.addRequired(); AU.addRequired(); + AU.addUsedIfAvailable(); AU.addPreserved(); @@ -187,6 +193,7 @@ AU.addPreserved(); AU.addPreserved(); AU.addPreserved(); + AU.addPreserved(); // FIXME: We do not yet add regions for the newly generated code to the // region tree. Index: lib/CodeGen/CodegenCleanup.cpp =================================================================== --- /dev/null +++ lib/CodeGen/CodegenCleanup.cpp @@ -0,0 +1,134 @@ +#include "polly/CodeGen/CodegenCleanup.h" + +#include "llvm/Analysis/CFLAliasAnalysis.h" +#include "llvm/Analysis/ScopedNoAliasAA.h" +#include "llvm/Analysis/TypeBasedAliasAnalysis.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/LegacyPassManager.h" +#include "llvm/PassInfo.h" +#include "llvm/PassRegistry.h" +#include "llvm/PassSupport.h" +#include "llvm/Support/Debug.h" +#include "llvm/Transforms/Scalar.h" +#define DEBUG_TYPE "polly-cleanup" + +using namespace llvm; +using namespace polly; + +namespace { + +class CodegenCleanup : public FunctionPass { +private: + CodegenCleanup(const CodegenCleanup &) = delete; + const CodegenCleanup &operator=(const CodegenCleanup &) = delete; + + llvm::legacy::FunctionPassManager *FPM; + +public: + static char ID; + explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {} + //~CodegenCleanup() { releaseMemory(); } + + /// @name FunctionPass interface + //@{ + virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { + AU.addRequired(); + // AU.addUsedIfAvailable + } + + virtual bool doInitialization(Module &M) override { + assert(!FPM); + + FPM = new llvm::legacy::FunctionPassManager(&M); + + // TODO: How to make parent passes discoverable? + FPM->add(createCFLAAWrapperPass()); + FPM->add(createScopedNoAliasAAWrapperPass()); + FPM->add(createTypeBasedAAWrapperPass()); + FPM->add(createAAResultsWrapperPass()); + + // TODO: Should be sensitive to compiler options + FPM->add(createCFGSimplificationPass()); + FPM->add(createScalarReplAggregatesPass()); + FPM->add(createEarlyCSEPass()); + FPM->add(createInstructionCombiningPass()); + FPM->add(createJumpThreadingPass()); + FPM->add(createCorrelatedValuePropagationPass()); + FPM->add(createCFGSimplificationPass()); + FPM->add(createInstructionCombiningPass()); + FPM->add(createCFGSimplificationPass()); + FPM->add(createReassociatePass()); + FPM->add(createLoopRotatePass()); + FPM->add(createLICMPass()); + FPM->add(createLoopUnswitchPass()); + FPM->add(createCFGSimplificationPass()); + FPM->add(createInstructionCombiningPass()); + FPM->add(createIndVarSimplifyPass()); + FPM->add(createLoopIdiomPass()); + FPM->add(createLoopDeletionPass()); + FPM->add(createLoopInterchangePass()); + FPM->add(createCFGSimplificationPass()); + FPM->add(createSimpleLoopUnrollPass()); + FPM->add(createMergedLoadStoreMotionPass()); + FPM->add(createMemCpyOptPass()); + FPM->add(createBitTrackingDCEPass()); + FPM->add(createInstructionCombiningPass()); + FPM->add(createJumpThreadingPass()); + FPM->add(createCorrelatedValuePropagationPass()); + FPM->add(createDeadStoreEliminationPass()); + FPM->add(createLICMPass()); + FPM->add(createLoopRerollPass()); + FPM->add(createLoadCombinePass()); + FPM->add(createAggressiveDCEPass()); + FPM->add(createCFGSimplificationPass()); + FPM->add(createInstructionCombiningPass()); + + return FPM->doInitialization(); + } + + virtual bool doFinalization(Module &M) override { + auto Result = FPM->doFinalization(); + + delete FPM; + FPM = nullptr; + + return Result; + } + + void releaseMemory() override {} + + virtual bool runOnFunction(llvm::Function &F) override { + auto &Remembered = getAnalysis(); + if (!Remembered.hasChanged()) + return false; + DEBUG(dbgs() << "Running Codegen Cleanup..."); + return FPM->run(F); + } + //@} +}; + +char CodegenCleanup::ID; +} + +FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); } + +INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup", + "Polly - Cleanup after codegeneration", false, false); +INITIALIZE_PASS_DEPENDENCY(RememberCodegen); +INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup", + "Polly - Cleanup after codegeneration", false, false); + +char RememberCodegen::ID; + +void RememberCodegen::getAnalysisUsage(llvm::AnalysisUsage &AU) const { + AU.setPreservesAll(); +} + +FunctionPass *polly::createRememberCodegenPass() { + return new RememberCodegen(); +} + +INITIALIZE_PASS_BEGIN(RememberCodegen, "polly-remember", + "Polly - Remember to cleanup after Codegen", false, true); +INITIALIZE_PASS_END(RememberCodegen, "polly-remember", + "Polly - Remember to cleanup after Codegen", false, true); Index: lib/Support/RegisterPasses.cpp =================================================================== --- lib/Support/RegisterPasses.cpp +++ lib/Support/RegisterPasses.cpp @@ -22,6 +22,7 @@ #include "polly/RegisterPasses.h" #include "polly/Canonicalization.h" #include "polly/CodeGen/CodeGeneration.h" +#include "polly/CodeGen/CodegenCleanup.h" #include "polly/DependenceInfo.h" #include "polly/LinkAllPasses.h" #include "polly/Options.h" @@ -153,6 +154,8 @@ initializePollyCanonicalizePass(Registry); initializeScopDetectionPass(Registry); initializeScopInfoPass(Registry); + initializeRememberCodegenPass(Registry); + initializeCodegenCleanupPass(Registry); } /// @brief Register Polly passes such that they form a polyhedral optimizer. @@ -262,8 +265,9 @@ return; PM.add(polly::createCodePreparationPass()); + PM.add(polly::createRememberCodegenPass()); polly::registerPollyPasses(PM); - // TODO: Add some cleanup passes + PM.add(createCodegenCleanupPass()); } static void @@ -276,8 +280,9 @@ return; PM.add(polly::createCodePreparationPass()); + PM.add(polly::createRememberCodegenPass()); polly::registerPollyPasses(PM); - // TODO: Add some cleanup passes + PM.add(createCodegenCleanupPass()); } /// @brief Register Polly to be available as an optimizer Index: test/ScopInfo/new-domain-construction-schedule-not-freed.ll =================================================================== --- test/ScopInfo/new-domain-construction-schedule-not-freed.ll +++ test/ScopInfo/new-domain-construction-schedule-not-freed.ll @@ -1,10 +1,8 @@ -; RUN: opt %loadPolly -analyze -polly-scops %s | FileCheck %s +; RUN: opt %loadPolly -analyze -polly-scops %s ; ; At some point this caused a problem in the schedule generation and we ; keep the test to avoid regressions there. ; -; CHECK: Statements -; target triple = "x86_64-unknown-linux-gnu" ; Function Attrs: nounwind uwtable