1 //===- CodegenCleanup.cpp -------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "polly/CodeGen/CodegenCleanup.h" 11 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 #include "llvm/Transforms/Scalar/GVN.h" 22 23 #define DEBUG_TYPE "polly-cleanup" 24 25 using namespace llvm; 26 using namespace polly; 27 28 namespace { 29 30 class CodegenCleanup : public FunctionPass { 31 private: 32 CodegenCleanup(const CodegenCleanup &) = delete; 33 const CodegenCleanup &operator=(const CodegenCleanup &) = delete; 34 35 llvm::legacy::FunctionPassManager *FPM; 36 37 public: 38 static char ID; 39 explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {} 40 41 /// @name FunctionPass interface 42 //@{ 43 virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {} 44 45 virtual bool doInitialization(Module &M) override { 46 assert(!FPM); 47 48 FPM = new llvm::legacy::FunctionPassManager(&M); 49 50 // TODO: How to make parent passes discoverable? 51 // TODO: Should be sensitive to compiler options in PassManagerBuilder, to 52 // which we do not have access here. 53 FPM->add(createScopedNoAliasAAWrapperPass()); 54 FPM->add(createTypeBasedAAWrapperPass()); 55 FPM->add(createAAResultsWrapperPass()); 56 57 // TODO: These are non-conditional passes that run between 58 // EP_ModuleOptimizerEarly and EP_VectorizerStart just to ensure we do not 59 // miss any optimization that would have run after Polly with 60 // -polly-position=early. This can probably be reduced to a more compact set 61 // of passes. 62 FPM->add(createCFGSimplificationPass()); 63 FPM->add(createSROAPass()); 64 FPM->add(createEarlyCSEPass()); 65 66 FPM->add(createPromoteMemoryToRegisterPass()); 67 FPM->add(createInstructionCombiningPass(true)); 68 FPM->add(createCFGSimplificationPass()); 69 FPM->add(createSROAPass()); 70 FPM->add(createEarlyCSEPass(true)); 71 FPM->add(createSpeculativeExecutionIfHasBranchDivergencePass()); 72 FPM->add(createJumpThreadingPass()); 73 FPM->add(createCorrelatedValuePropagationPass()); 74 FPM->add(createCFGSimplificationPass()); 75 FPM->add(createInstructionCombiningPass(true)); 76 FPM->add(createLibCallsShrinkWrapPass()); 77 FPM->add(createTailCallEliminationPass()); 78 FPM->add(createCFGSimplificationPass()); 79 FPM->add(createReassociatePass()); 80 FPM->add(createLoopRotatePass(-1)); 81 FPM->add(createGVNPass()); 82 FPM->add(createLICMPass()); 83 FPM->add(createLoopUnswitchPass()); 84 FPM->add(createCFGSimplificationPass()); 85 FPM->add(createInstructionCombiningPass(true)); 86 FPM->add(createIndVarSimplifyPass()); 87 FPM->add(createLoopIdiomPass()); 88 FPM->add(createLoopDeletionPass()); 89 FPM->add(createCFGSimplificationPass()); 90 FPM->add(createSimpleLoopUnrollPass(3)); 91 FPM->add(createMergedLoadStoreMotionPass()); 92 FPM->add(createGVNPass()); 93 FPM->add(createMemCpyOptPass()); 94 FPM->add(createSCCPPass()); 95 FPM->add(createBitTrackingDCEPass()); 96 FPM->add(createInstructionCombiningPass(true)); 97 FPM->add(createJumpThreadingPass()); 98 FPM->add(createCorrelatedValuePropagationPass()); 99 FPM->add(createDeadStoreEliminationPass()); 100 FPM->add(createLICMPass()); 101 FPM->add(createAggressiveDCEPass()); 102 FPM->add(createCFGSimplificationPass()); 103 FPM->add(createInstructionCombiningPass(true)); 104 FPM->add(createFloat2IntPass()); 105 106 return FPM->doInitialization(); 107 } 108 109 virtual bool doFinalization(Module &M) override { 110 bool Result = FPM->doFinalization(); 111 112 delete FPM; 113 FPM = nullptr; 114 115 return Result; 116 } 117 118 virtual bool runOnFunction(llvm::Function &F) override { 119 if (!F.hasFnAttribute("polly-optimized")) { 120 DEBUG(dbgs() << F.getName() 121 << ": Skipping cleanup because Polly did not optimize it."); 122 return false; 123 } 124 125 DEBUG(dbgs() << F.getName() << ": Running codegen cleanup..."); 126 return FPM->run(F); 127 } 128 //@} 129 }; 130 131 char CodegenCleanup::ID; 132 } // namespace 133 134 FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); } 135 136 INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup", 137 "Polly - Cleanup after code generation", false, false) 138 INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup", 139 "Polly - Cleanup after code generation", false, false) 140