1 //===- CodegenCleanup.cpp -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "polly/CodeGen/CodegenCleanup.h"
12 #include "llvm/Analysis/ScopedNoAliasAA.h"
13 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/LegacyPassManager.h"
16 #include "llvm/PassInfo.h"
17 #include "llvm/PassRegistry.h"
18 #include "llvm/PassSupport.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Transforms/Scalar.h"
21 #define DEBUG_TYPE "polly-cleanup"
24 using namespace polly
;
28 class CodegenCleanup
: public FunctionPass
{
30 CodegenCleanup(const CodegenCleanup
&) = delete;
31 const CodegenCleanup
&operator=(const CodegenCleanup
&) = delete;
33 llvm::legacy::FunctionPassManager
*FPM
;
37 explicit CodegenCleanup() : FunctionPass(ID
), FPM(nullptr) {}
39 /// @name FunctionPass interface
41 virtual void getAnalysisUsage(llvm::AnalysisUsage
&AU
) const override
{}
43 virtual bool doInitialization(Module
&M
) override
{
46 FPM
= new llvm::legacy::FunctionPassManager(&M
);
48 // TODO: How to make parent passes discoverable?
49 // TODO: Should be sensitive to compiler options in PassManagerBuilder, to
50 // which we do not have access here.
51 FPM
->add(createScopedNoAliasAAWrapperPass());
52 FPM
->add(createTypeBasedAAWrapperPass());
53 FPM
->add(createAAResultsWrapperPass());
55 // TODO: These are non-conditional passes that run between
56 // EP_ModuleOptimizerEarly and EP_VectorizerStart just to ensure we do not
57 // miss any optimization that would have run after Polly with
58 // -polly-position=early. This can probably be reduced to a more compact set
60 FPM
->add(createCFGSimplificationPass());
61 FPM
->add(createSROAPass());
62 FPM
->add(createEarlyCSEPass());
63 FPM
->add(createInstructionCombiningPass());
64 FPM
->add(createJumpThreadingPass());
65 FPM
->add(createCorrelatedValuePropagationPass());
66 FPM
->add(createCFGSimplificationPass());
67 FPM
->add(createInstructionCombiningPass());
68 FPM
->add(createCFGSimplificationPass());
69 FPM
->add(createReassociatePass());
70 FPM
->add(createLoopRotatePass());
71 FPM
->add(createLICMPass());
72 FPM
->add(createLoopUnswitchPass());
73 FPM
->add(createCFGSimplificationPass());
74 FPM
->add(createInstructionCombiningPass());
75 FPM
->add(createIndVarSimplifyPass());
76 FPM
->add(createLoopIdiomPass());
77 FPM
->add(createLoopDeletionPass());
78 FPM
->add(createCFGSimplificationPass());
79 FPM
->add(createSimpleLoopUnrollPass());
80 FPM
->add(createMergedLoadStoreMotionPass());
81 FPM
->add(createMemCpyOptPass());
82 FPM
->add(createBitTrackingDCEPass());
83 FPM
->add(createInstructionCombiningPass());
84 FPM
->add(createJumpThreadingPass());
85 FPM
->add(createCorrelatedValuePropagationPass());
86 FPM
->add(createDeadStoreEliminationPass());
87 FPM
->add(createLICMPass());
88 FPM
->add(createLoopRerollPass());
89 FPM
->add(createAggressiveDCEPass());
90 FPM
->add(createCFGSimplificationPass());
91 FPM
->add(createInstructionCombiningPass());
93 return FPM
->doInitialization();
96 virtual bool doFinalization(Module
&M
) override
{
97 bool Result
= FPM
->doFinalization();
105 virtual bool runOnFunction(llvm::Function
&F
) override
{
106 if (!F
.hasFnAttribute("polly-optimized")) {
107 DEBUG(dbgs() << F
.getName()
108 << ": Skipping cleanup because Polly did not optimize it.");
112 DEBUG(dbgs() << F
.getName() << ": Running codegen cleanup...");
118 char CodegenCleanup::ID
;
121 FunctionPass
*polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
123 INITIALIZE_PASS_BEGIN(CodegenCleanup
, "polly-cleanup",
124 "Polly - Cleanup after code generation", false, false)
125 INITIALIZE_PASS_END(CodegenCleanup
, "polly-cleanup",
126 "Polly - Cleanup after code generation", false, false)