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"
15*39f6f296SSimon 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"
20757c8cf6SReid Kleckner #include "llvm/Transforms/Utils.h"
211a695b1dSTobias Grosser 
224c86a1d9SMichael Kruse #define DEBUG_TYPE "polly-cleanup"
234c86a1d9SMichael Kruse 
244c86a1d9SMichael Kruse using namespace llvm;
254c86a1d9SMichael Kruse using namespace polly;
264c86a1d9SMichael Kruse 
274c86a1d9SMichael Kruse namespace {
284c86a1d9SMichael Kruse 
294c86a1d9SMichael Kruse class CodegenCleanup : public FunctionPass {
304c86a1d9SMichael Kruse private:
314c86a1d9SMichael Kruse   CodegenCleanup(const CodegenCleanup &) = delete;
324c86a1d9SMichael Kruse   const CodegenCleanup &operator=(const CodegenCleanup &) = delete;
334c86a1d9SMichael Kruse 
344c86a1d9SMichael Kruse   llvm::legacy::FunctionPassManager *FPM;
354c86a1d9SMichael Kruse 
364c86a1d9SMichael Kruse public:
374c86a1d9SMichael Kruse   static char ID;
384c86a1d9SMichael Kruse   explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {}
394c86a1d9SMichael Kruse 
404c86a1d9SMichael Kruse   /// @name FunctionPass interface
414c86a1d9SMichael Kruse   //@{
424c86a1d9SMichael Kruse   virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {}
434c86a1d9SMichael Kruse 
444c86a1d9SMichael Kruse   virtual bool doInitialization(Module &M) override {
454c86a1d9SMichael Kruse     assert(!FPM);
464c86a1d9SMichael Kruse 
474c86a1d9SMichael Kruse     FPM = new llvm::legacy::FunctionPassManager(&M);
484c86a1d9SMichael Kruse 
494c86a1d9SMichael Kruse     // TODO: How to make parent passes discoverable?
504c86a1d9SMichael Kruse     // TODO: Should be sensitive to compiler options in PassManagerBuilder, to
51a6d48f59SMichael Kruse     // which we do not have access here.
524c86a1d9SMichael Kruse     FPM->add(createScopedNoAliasAAWrapperPass());
534c86a1d9SMichael Kruse     FPM->add(createTypeBasedAAWrapperPass());
544c86a1d9SMichael Kruse     FPM->add(createAAResultsWrapperPass());
554c86a1d9SMichael Kruse 
564c86a1d9SMichael Kruse     // TODO: These are non-conditional passes that run between
574c86a1d9SMichael Kruse     // EP_ModuleOptimizerEarly and EP_VectorizerStart just to ensure we do not
584c86a1d9SMichael Kruse     // miss any optimization that would have run after Polly with
594c86a1d9SMichael Kruse     // -polly-position=early. This can probably be reduced to a more compact set
604c86a1d9SMichael Kruse     // of passes.
614c86a1d9SMichael Kruse     FPM->add(createCFGSimplificationPass());
626b4e9282SMichael Kruse     FPM->add(createSROAPass());
634c86a1d9SMichael Kruse     FPM->add(createEarlyCSEPass());
640481d78cSMichael Kruse 
650481d78cSMichael Kruse     FPM->add(createPromoteMemoryToRegisterPass());
660481d78cSMichael Kruse     FPM->add(createInstructionCombiningPass(true));
670481d78cSMichael Kruse     FPM->add(createCFGSimplificationPass());
680481d78cSMichael Kruse     FPM->add(createSROAPass());
690481d78cSMichael Kruse     FPM->add(createEarlyCSEPass(true));
700481d78cSMichael Kruse     FPM->add(createSpeculativeExecutionIfHasBranchDivergencePass());
714c86a1d9SMichael Kruse     FPM->add(createJumpThreadingPass());
724c86a1d9SMichael Kruse     FPM->add(createCorrelatedValuePropagationPass());
734c86a1d9SMichael Kruse     FPM->add(createCFGSimplificationPass());
740481d78cSMichael Kruse     FPM->add(createInstructionCombiningPass(true));
750481d78cSMichael Kruse     FPM->add(createLibCallsShrinkWrapPass());
760481d78cSMichael Kruse     FPM->add(createTailCallEliminationPass());
774c86a1d9SMichael Kruse     FPM->add(createCFGSimplificationPass());
784c86a1d9SMichael Kruse     FPM->add(createReassociatePass());
790481d78cSMichael Kruse     FPM->add(createLoopRotatePass(-1));
801a695b1dSTobias Grosser     FPM->add(createGVNPass());
814c86a1d9SMichael Kruse     FPM->add(createLICMPass());
824c86a1d9SMichael Kruse     FPM->add(createLoopUnswitchPass());
834c86a1d9SMichael Kruse     FPM->add(createCFGSimplificationPass());
840481d78cSMichael Kruse     FPM->add(createInstructionCombiningPass(true));
854c86a1d9SMichael Kruse     FPM->add(createIndVarSimplifyPass());
864c86a1d9SMichael Kruse     FPM->add(createLoopIdiomPass());
874c86a1d9SMichael Kruse     FPM->add(createLoopDeletionPass());
884c86a1d9SMichael Kruse     FPM->add(createCFGSimplificationPass());
890481d78cSMichael Kruse     FPM->add(createSimpleLoopUnrollPass(3));
904c86a1d9SMichael Kruse     FPM->add(createMergedLoadStoreMotionPass());
910481d78cSMichael Kruse     FPM->add(createGVNPass());
924c86a1d9SMichael Kruse     FPM->add(createMemCpyOptPass());
930481d78cSMichael Kruse     FPM->add(createSCCPPass());
944c86a1d9SMichael Kruse     FPM->add(createBitTrackingDCEPass());
950481d78cSMichael Kruse     FPM->add(createInstructionCombiningPass(true));
964c86a1d9SMichael Kruse     FPM->add(createJumpThreadingPass());
974c86a1d9SMichael Kruse     FPM->add(createCorrelatedValuePropagationPass());
984c86a1d9SMichael Kruse     FPM->add(createDeadStoreEliminationPass());
994c86a1d9SMichael Kruse     FPM->add(createLICMPass());
1004c86a1d9SMichael Kruse     FPM->add(createAggressiveDCEPass());
1014c86a1d9SMichael Kruse     FPM->add(createCFGSimplificationPass());
1020481d78cSMichael Kruse     FPM->add(createInstructionCombiningPass(true));
1030481d78cSMichael Kruse     FPM->add(createFloat2IntPass());
1044c86a1d9SMichael Kruse 
1054c86a1d9SMichael Kruse     return FPM->doInitialization();
1064c86a1d9SMichael Kruse   }
1074c86a1d9SMichael Kruse 
1084c86a1d9SMichael Kruse   virtual bool doFinalization(Module &M) override {
1094c86a1d9SMichael Kruse     bool Result = FPM->doFinalization();
1104c86a1d9SMichael Kruse 
1114c86a1d9SMichael Kruse     delete FPM;
1124c86a1d9SMichael Kruse     FPM = nullptr;
1134c86a1d9SMichael Kruse 
1144c86a1d9SMichael Kruse     return Result;
1154c86a1d9SMichael Kruse   }
1164c86a1d9SMichael Kruse 
1174c86a1d9SMichael Kruse   virtual bool runOnFunction(llvm::Function &F) override {
1184c86a1d9SMichael Kruse     if (!F.hasFnAttribute("polly-optimized")) {
119349506a9SNicola Zaghen       LLVM_DEBUG(
120349506a9SNicola Zaghen           dbgs() << F.getName()
1214c86a1d9SMichael Kruse                  << ": Skipping cleanup because Polly did not optimize it.");
1224c86a1d9SMichael Kruse       return false;
1234c86a1d9SMichael Kruse     }
1244c86a1d9SMichael Kruse 
125349506a9SNicola Zaghen     LLVM_DEBUG(dbgs() << F.getName() << ": Running codegen cleanup...");
1264c86a1d9SMichael Kruse     return FPM->run(F);
1274c86a1d9SMichael Kruse   }
1284c86a1d9SMichael Kruse   //@}
1294c86a1d9SMichael Kruse };
1304c86a1d9SMichael Kruse 
1314c86a1d9SMichael Kruse char CodegenCleanup::ID;
132522478d2STobias Grosser } // namespace
1334c86a1d9SMichael Kruse 
1344c86a1d9SMichael Kruse FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
1354c86a1d9SMichael Kruse 
1364c86a1d9SMichael Kruse INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup",
1374c86a1d9SMichael Kruse                       "Polly - Cleanup after code generation", false, false)
1384c86a1d9SMichael Kruse INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup",
1394c86a1d9SMichael Kruse                     "Polly - Cleanup after code generation", false, false)
140