1616449dfSTobias Grosser //===- CodegenCleanup.cpp -------------------------------------------------===// 2616449dfSTobias Grosser // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6616449dfSTobias Grosser // 7616449dfSTobias Grosser //===----------------------------------------------------------------------===// 8616449dfSTobias Grosser 94c86a1d9SMichael Kruse #include "polly/CodeGen/CodegenCleanup.h" 104c86a1d9SMichael Kruse 114c86a1d9SMichael Kruse #include "llvm/Analysis/ScopedNoAliasAA.h" 124c86a1d9SMichael Kruse #include "llvm/Analysis/TypeBasedAliasAnalysis.h" 134c86a1d9SMichael Kruse #include "llvm/IR/Function.h" 144c86a1d9SMichael Kruse #include "llvm/IR/LegacyPassManager.h" 154c86a1d9SMichael Kruse #include "llvm/PassInfo.h" 164c86a1d9SMichael Kruse #include "llvm/PassRegistry.h" 174c86a1d9SMichael Kruse #include "llvm/PassSupport.h" 184c86a1d9SMichael Kruse #include "llvm/Support/Debug.h" 1960dc462bSDavid Blaikie #include "llvm/Transforms/InstCombine/InstCombine.h" 204c86a1d9SMichael Kruse #include "llvm/Transforms/Scalar.h" 211a695b1dSTobias Grosser #include "llvm/Transforms/Scalar/GVN.h" 22757c8cf6SReid Kleckner #include "llvm/Transforms/Utils.h" 231a695b1dSTobias Grosser 244c86a1d9SMichael Kruse #define DEBUG_TYPE "polly-cleanup" 254c86a1d9SMichael Kruse 264c86a1d9SMichael Kruse using namespace llvm; 274c86a1d9SMichael Kruse using namespace polly; 284c86a1d9SMichael Kruse 294c86a1d9SMichael Kruse namespace { 304c86a1d9SMichael Kruse 314c86a1d9SMichael Kruse class CodegenCleanup : public FunctionPass { 324c86a1d9SMichael Kruse private: 334c86a1d9SMichael Kruse CodegenCleanup(const CodegenCleanup &) = delete; 344c86a1d9SMichael Kruse const CodegenCleanup &operator=(const CodegenCleanup &) = delete; 354c86a1d9SMichael Kruse 364c86a1d9SMichael Kruse llvm::legacy::FunctionPassManager *FPM; 374c86a1d9SMichael Kruse 384c86a1d9SMichael Kruse public: 394c86a1d9SMichael Kruse static char ID; 404c86a1d9SMichael Kruse explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {} 414c86a1d9SMichael Kruse 424c86a1d9SMichael Kruse /// @name FunctionPass interface 434c86a1d9SMichael Kruse //@{ 444c86a1d9SMichael Kruse virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {} 454c86a1d9SMichael Kruse 464c86a1d9SMichael Kruse virtual bool doInitialization(Module &M) override { 474c86a1d9SMichael Kruse assert(!FPM); 484c86a1d9SMichael Kruse 494c86a1d9SMichael Kruse FPM = new llvm::legacy::FunctionPassManager(&M); 504c86a1d9SMichael Kruse 514c86a1d9SMichael Kruse // TODO: How to make parent passes discoverable? 524c86a1d9SMichael Kruse // TODO: Should be sensitive to compiler options in PassManagerBuilder, to 53a6d48f59SMichael Kruse // which we do not have access here. 544c86a1d9SMichael Kruse FPM->add(createScopedNoAliasAAWrapperPass()); 554c86a1d9SMichael Kruse FPM->add(createTypeBasedAAWrapperPass()); 564c86a1d9SMichael Kruse FPM->add(createAAResultsWrapperPass()); 574c86a1d9SMichael Kruse 584c86a1d9SMichael Kruse // TODO: These are non-conditional passes that run between 594c86a1d9SMichael Kruse // EP_ModuleOptimizerEarly and EP_VectorizerStart just to ensure we do not 604c86a1d9SMichael Kruse // miss any optimization that would have run after Polly with 614c86a1d9SMichael Kruse // -polly-position=early. This can probably be reduced to a more compact set 624c86a1d9SMichael Kruse // of passes. 634c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 646b4e9282SMichael Kruse FPM->add(createSROAPass()); 654c86a1d9SMichael Kruse FPM->add(createEarlyCSEPass()); 660481d78cSMichael Kruse 670481d78cSMichael Kruse FPM->add(createPromoteMemoryToRegisterPass()); 680481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 690481d78cSMichael Kruse FPM->add(createCFGSimplificationPass()); 700481d78cSMichael Kruse FPM->add(createSROAPass()); 710481d78cSMichael Kruse FPM->add(createEarlyCSEPass(true)); 720481d78cSMichael Kruse FPM->add(createSpeculativeExecutionIfHasBranchDivergencePass()); 734c86a1d9SMichael Kruse FPM->add(createJumpThreadingPass()); 744c86a1d9SMichael Kruse FPM->add(createCorrelatedValuePropagationPass()); 754c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 760481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 770481d78cSMichael Kruse FPM->add(createLibCallsShrinkWrapPass()); 780481d78cSMichael Kruse FPM->add(createTailCallEliminationPass()); 794c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 804c86a1d9SMichael Kruse FPM->add(createReassociatePass()); 810481d78cSMichael Kruse FPM->add(createLoopRotatePass(-1)); 821a695b1dSTobias Grosser FPM->add(createGVNPass()); 834c86a1d9SMichael Kruse FPM->add(createLICMPass()); 844c86a1d9SMichael Kruse FPM->add(createLoopUnswitchPass()); 854c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 860481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 874c86a1d9SMichael Kruse FPM->add(createIndVarSimplifyPass()); 884c86a1d9SMichael Kruse FPM->add(createLoopIdiomPass()); 894c86a1d9SMichael Kruse FPM->add(createLoopDeletionPass()); 904c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 910481d78cSMichael Kruse FPM->add(createSimpleLoopUnrollPass(3)); 924c86a1d9SMichael Kruse FPM->add(createMergedLoadStoreMotionPass()); 930481d78cSMichael Kruse FPM->add(createGVNPass()); 944c86a1d9SMichael Kruse FPM->add(createMemCpyOptPass()); 950481d78cSMichael Kruse FPM->add(createSCCPPass()); 964c86a1d9SMichael Kruse FPM->add(createBitTrackingDCEPass()); 970481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 984c86a1d9SMichael Kruse FPM->add(createJumpThreadingPass()); 994c86a1d9SMichael Kruse FPM->add(createCorrelatedValuePropagationPass()); 1004c86a1d9SMichael Kruse FPM->add(createDeadStoreEliminationPass()); 1014c86a1d9SMichael Kruse FPM->add(createLICMPass()); 1024c86a1d9SMichael Kruse FPM->add(createAggressiveDCEPass()); 1034c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 1040481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 1050481d78cSMichael Kruse FPM->add(createFloat2IntPass()); 1064c86a1d9SMichael Kruse 1074c86a1d9SMichael Kruse return FPM->doInitialization(); 1084c86a1d9SMichael Kruse } 1094c86a1d9SMichael Kruse 1104c86a1d9SMichael Kruse virtual bool doFinalization(Module &M) override { 1114c86a1d9SMichael Kruse bool Result = FPM->doFinalization(); 1124c86a1d9SMichael Kruse 1134c86a1d9SMichael Kruse delete FPM; 1144c86a1d9SMichael Kruse FPM = nullptr; 1154c86a1d9SMichael Kruse 1164c86a1d9SMichael Kruse return Result; 1174c86a1d9SMichael Kruse } 1184c86a1d9SMichael Kruse 1194c86a1d9SMichael Kruse virtual bool runOnFunction(llvm::Function &F) override { 1204c86a1d9SMichael Kruse if (!F.hasFnAttribute("polly-optimized")) { 121349506a9SNicola Zaghen LLVM_DEBUG( 122349506a9SNicola Zaghen dbgs() << F.getName() 1234c86a1d9SMichael Kruse << ": Skipping cleanup because Polly did not optimize it."); 1244c86a1d9SMichael Kruse return false; 1254c86a1d9SMichael Kruse } 1264c86a1d9SMichael Kruse 127349506a9SNicola Zaghen LLVM_DEBUG(dbgs() << F.getName() << ": Running codegen cleanup..."); 1284c86a1d9SMichael Kruse return FPM->run(F); 1294c86a1d9SMichael Kruse } 1304c86a1d9SMichael Kruse //@} 1314c86a1d9SMichael Kruse }; 1324c86a1d9SMichael Kruse 1334c86a1d9SMichael Kruse char CodegenCleanup::ID; 134522478d2STobias Grosser } // namespace 1354c86a1d9SMichael Kruse 1364c86a1d9SMichael Kruse FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); } 1374c86a1d9SMichael Kruse 1384c86a1d9SMichael Kruse INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup", 1394c86a1d9SMichael Kruse "Polly - Cleanup after code generation", false, false) 1404c86a1d9SMichael Kruse INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup", 1414c86a1d9SMichael Kruse "Polly - Cleanup after code generation", false, false) 142