1616449dfSTobias Grosser //===- CodegenCleanup.cpp -------------------------------------------------===// 2616449dfSTobias Grosser // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler 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" 1539f6f296SSimon Pilgrim #include "llvm/Pass.h" 164c86a1d9SMichael Kruse #include "llvm/Support/Debug.h" 1760dc462bSDavid Blaikie #include "llvm/Transforms/InstCombine/InstCombine.h" 184c86a1d9SMichael Kruse #include "llvm/Transforms/Scalar.h" 191a695b1dSTobias Grosser #include "llvm/Transforms/Scalar/GVN.h" 20*fb4113efSFlorian Hahn #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" 21757c8cf6SReid Kleckner #include "llvm/Transforms/Utils.h" 221a695b1dSTobias Grosser 234c86a1d9SMichael Kruse #define DEBUG_TYPE "polly-cleanup" 244c86a1d9SMichael Kruse 254c86a1d9SMichael Kruse using namespace llvm; 264c86a1d9SMichael Kruse using namespace polly; 274c86a1d9SMichael Kruse 284c86a1d9SMichael Kruse namespace { 294c86a1d9SMichael Kruse 304c86a1d9SMichael Kruse class CodegenCleanup : public FunctionPass { 314c86a1d9SMichael Kruse private: 324c86a1d9SMichael Kruse CodegenCleanup(const CodegenCleanup &) = delete; 334c86a1d9SMichael Kruse const CodegenCleanup &operator=(const CodegenCleanup &) = delete; 344c86a1d9SMichael Kruse 354c86a1d9SMichael Kruse llvm::legacy::FunctionPassManager *FPM; 364c86a1d9SMichael Kruse 374c86a1d9SMichael Kruse public: 384c86a1d9SMichael Kruse static char ID; 394c86a1d9SMichael Kruse explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {} 404c86a1d9SMichael Kruse 414c86a1d9SMichael Kruse /// @name FunctionPass interface 424c86a1d9SMichael Kruse //@{ 434c86a1d9SMichael Kruse virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {} 444c86a1d9SMichael Kruse 454c86a1d9SMichael Kruse virtual bool doInitialization(Module &M) override { 464c86a1d9SMichael Kruse assert(!FPM); 474c86a1d9SMichael Kruse 484c86a1d9SMichael Kruse FPM = new llvm::legacy::FunctionPassManager(&M); 494c86a1d9SMichael Kruse 504c86a1d9SMichael Kruse // TODO: How to make parent passes discoverable? 514c86a1d9SMichael Kruse // TODO: Should be sensitive to compiler options in PassManagerBuilder, to 52a6d48f59SMichael Kruse // which we do not have access here. 534c86a1d9SMichael Kruse FPM->add(createScopedNoAliasAAWrapperPass()); 544c86a1d9SMichael Kruse FPM->add(createTypeBasedAAWrapperPass()); 554c86a1d9SMichael Kruse FPM->add(createAAResultsWrapperPass()); 564c86a1d9SMichael Kruse 574c86a1d9SMichael Kruse // TODO: These are non-conditional passes that run between 584c86a1d9SMichael Kruse // EP_ModuleOptimizerEarly and EP_VectorizerStart just to ensure we do not 594c86a1d9SMichael Kruse // miss any optimization that would have run after Polly with 604c86a1d9SMichael Kruse // -polly-position=early. This can probably be reduced to a more compact set 614c86a1d9SMichael Kruse // of passes. 624c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 636b4e9282SMichael Kruse FPM->add(createSROAPass()); 644c86a1d9SMichael Kruse FPM->add(createEarlyCSEPass()); 650481d78cSMichael Kruse 660481d78cSMichael Kruse FPM->add(createPromoteMemoryToRegisterPass()); 670481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 680481d78cSMichael Kruse FPM->add(createCFGSimplificationPass()); 690481d78cSMichael Kruse FPM->add(createSROAPass()); 700481d78cSMichael Kruse FPM->add(createEarlyCSEPass(true)); 710481d78cSMichael Kruse FPM->add(createSpeculativeExecutionIfHasBranchDivergencePass()); 724c86a1d9SMichael Kruse FPM->add(createJumpThreadingPass()); 734c86a1d9SMichael Kruse FPM->add(createCorrelatedValuePropagationPass()); 744c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 750481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 760481d78cSMichael Kruse FPM->add(createLibCallsShrinkWrapPass()); 770481d78cSMichael Kruse FPM->add(createTailCallEliminationPass()); 784c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 794c86a1d9SMichael Kruse FPM->add(createReassociatePass()); 800481d78cSMichael Kruse FPM->add(createLoopRotatePass(-1)); 811a695b1dSTobias Grosser FPM->add(createGVNPass()); 824c86a1d9SMichael Kruse FPM->add(createLICMPass()); 83*fb4113efSFlorian Hahn FPM->add(createSimpleLoopUnswitchLegacyPass()); 844c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 850481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 864c86a1d9SMichael Kruse FPM->add(createIndVarSimplifyPass()); 874c86a1d9SMichael Kruse FPM->add(createLoopIdiomPass()); 884c86a1d9SMichael Kruse FPM->add(createLoopDeletionPass()); 894c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 900481d78cSMichael Kruse FPM->add(createSimpleLoopUnrollPass(3)); 914c86a1d9SMichael Kruse FPM->add(createMergedLoadStoreMotionPass()); 920481d78cSMichael Kruse FPM->add(createGVNPass()); 934c86a1d9SMichael Kruse FPM->add(createMemCpyOptPass()); 940481d78cSMichael Kruse FPM->add(createSCCPPass()); 954c86a1d9SMichael Kruse FPM->add(createBitTrackingDCEPass()); 960481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 974c86a1d9SMichael Kruse FPM->add(createJumpThreadingPass()); 984c86a1d9SMichael Kruse FPM->add(createCorrelatedValuePropagationPass()); 994c86a1d9SMichael Kruse FPM->add(createDeadStoreEliminationPass()); 1004c86a1d9SMichael Kruse FPM->add(createLICMPass()); 1014c86a1d9SMichael Kruse FPM->add(createAggressiveDCEPass()); 1024c86a1d9SMichael Kruse FPM->add(createCFGSimplificationPass()); 1030481d78cSMichael Kruse FPM->add(createInstructionCombiningPass(true)); 1040481d78cSMichael Kruse FPM->add(createFloat2IntPass()); 1054c86a1d9SMichael Kruse 1064c86a1d9SMichael Kruse return FPM->doInitialization(); 1074c86a1d9SMichael Kruse } 1084c86a1d9SMichael Kruse 1094c86a1d9SMichael Kruse virtual bool doFinalization(Module &M) override { 1104c86a1d9SMichael Kruse bool Result = FPM->doFinalization(); 1114c86a1d9SMichael Kruse 1124c86a1d9SMichael Kruse delete FPM; 1134c86a1d9SMichael Kruse FPM = nullptr; 1144c86a1d9SMichael Kruse 1154c86a1d9SMichael Kruse return Result; 1164c86a1d9SMichael Kruse } 1174c86a1d9SMichael Kruse 1184c86a1d9SMichael Kruse virtual bool runOnFunction(llvm::Function &F) override { 1194c86a1d9SMichael Kruse if (!F.hasFnAttribute("polly-optimized")) { 120349506a9SNicola Zaghen LLVM_DEBUG( 121349506a9SNicola Zaghen dbgs() << F.getName() 1224c86a1d9SMichael Kruse << ": Skipping cleanup because Polly did not optimize it."); 1234c86a1d9SMichael Kruse return false; 1244c86a1d9SMichael Kruse } 1254c86a1d9SMichael Kruse 126349506a9SNicola Zaghen LLVM_DEBUG(dbgs() << F.getName() << ": Running codegen cleanup..."); 1274c86a1d9SMichael Kruse return FPM->run(F); 1284c86a1d9SMichael Kruse } 1294c86a1d9SMichael Kruse //@} 1304c86a1d9SMichael Kruse }; 1314c86a1d9SMichael Kruse 1324c86a1d9SMichael Kruse char CodegenCleanup::ID; 133522478d2STobias Grosser } // namespace 1344c86a1d9SMichael Kruse 1354c86a1d9SMichael Kruse FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); } 1364c86a1d9SMichael Kruse 1374c86a1d9SMichael Kruse INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup", 1384c86a1d9SMichael Kruse "Polly - Cleanup after code generation", false, false) 1394c86a1d9SMichael Kruse INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup", 1404c86a1d9SMichael Kruse "Polly - Cleanup after code generation", false, false) 141