2016-06-23 00:29:28 +08:00
|
|
|
//===- CodegenCleanup.cpp -------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-06-23 00:29:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-11-26 20:36:25 +08:00
|
|
|
#include "polly/CodeGen/CodegenCleanup.h"
|
|
|
|
|
|
|
|
#include "llvm/Analysis/ScopedNoAliasAA.h"
|
|
|
|
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
|
|
|
#include "llvm/PassSupport.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2018-04-24 10:23:41 +08:00
|
|
|
#include "llvm/Transforms/InstCombine/InstCombine.h"
|
2015-11-26 20:36:25 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2017-09-04 19:04:33 +08:00
|
|
|
#include "llvm/Transforms/Scalar/GVN.h"
|
2018-03-29 03:56:26 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
2017-09-04 19:04:33 +08:00
|
|
|
|
2015-11-26 20:36:25 +08:00
|
|
|
#define DEBUG_TYPE "polly-cleanup"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CodegenCleanup : public FunctionPass {
|
|
|
|
private:
|
|
|
|
CodegenCleanup(const CodegenCleanup &) = delete;
|
|
|
|
const CodegenCleanup &operator=(const CodegenCleanup &) = delete;
|
|
|
|
|
|
|
|
llvm::legacy::FunctionPassManager *FPM;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {}
|
|
|
|
|
|
|
|
/// @name FunctionPass interface
|
|
|
|
//@{
|
|
|
|
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {}
|
|
|
|
|
|
|
|
virtual bool doInitialization(Module &M) override {
|
|
|
|
assert(!FPM);
|
|
|
|
|
|
|
|
FPM = new llvm::legacy::FunctionPassManager(&M);
|
|
|
|
|
|
|
|
// TODO: How to make parent passes discoverable?
|
|
|
|
// TODO: Should be sensitive to compiler options in PassManagerBuilder, to
|
2017-06-08 20:06:15 +08:00
|
|
|
// which we do not have access here.
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createScopedNoAliasAAWrapperPass());
|
|
|
|
FPM->add(createTypeBasedAAWrapperPass());
|
|
|
|
FPM->add(createAAResultsWrapperPass());
|
|
|
|
|
|
|
|
// TODO: These are non-conditional passes that run between
|
|
|
|
// EP_ModuleOptimizerEarly and EP_VectorizerStart just to ensure we do not
|
|
|
|
// miss any optimization that would have run after Polly with
|
|
|
|
// -polly-position=early. This can probably be reduced to a more compact set
|
|
|
|
// of passes.
|
|
|
|
FPM->add(createCFGSimplificationPass());
|
2016-06-15 21:21:28 +08:00
|
|
|
FPM->add(createSROAPass());
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createEarlyCSEPass());
|
2017-09-10 05:43:49 +08:00
|
|
|
|
|
|
|
FPM->add(createPromoteMemoryToRegisterPass());
|
|
|
|
FPM->add(createInstructionCombiningPass(true));
|
|
|
|
FPM->add(createCFGSimplificationPass());
|
|
|
|
FPM->add(createSROAPass());
|
|
|
|
FPM->add(createEarlyCSEPass(true));
|
|
|
|
FPM->add(createSpeculativeExecutionIfHasBranchDivergencePass());
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createJumpThreadingPass());
|
|
|
|
FPM->add(createCorrelatedValuePropagationPass());
|
|
|
|
FPM->add(createCFGSimplificationPass());
|
2017-09-10 05:43:49 +08:00
|
|
|
FPM->add(createInstructionCombiningPass(true));
|
|
|
|
FPM->add(createLibCallsShrinkWrapPass());
|
|
|
|
FPM->add(createTailCallEliminationPass());
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createCFGSimplificationPass());
|
|
|
|
FPM->add(createReassociatePass());
|
2017-09-10 05:43:49 +08:00
|
|
|
FPM->add(createLoopRotatePass(-1));
|
2017-09-04 19:04:33 +08:00
|
|
|
FPM->add(createGVNPass());
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createLICMPass());
|
|
|
|
FPM->add(createLoopUnswitchPass());
|
|
|
|
FPM->add(createCFGSimplificationPass());
|
2017-09-10 05:43:49 +08:00
|
|
|
FPM->add(createInstructionCombiningPass(true));
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createIndVarSimplifyPass());
|
|
|
|
FPM->add(createLoopIdiomPass());
|
|
|
|
FPM->add(createLoopDeletionPass());
|
|
|
|
FPM->add(createCFGSimplificationPass());
|
2017-09-10 05:43:49 +08:00
|
|
|
FPM->add(createSimpleLoopUnrollPass(3));
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createMergedLoadStoreMotionPass());
|
2017-09-10 05:43:49 +08:00
|
|
|
FPM->add(createGVNPass());
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createMemCpyOptPass());
|
2017-09-10 05:43:49 +08:00
|
|
|
FPM->add(createSCCPPass());
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createBitTrackingDCEPass());
|
2017-09-10 05:43:49 +08:00
|
|
|
FPM->add(createInstructionCombiningPass(true));
|
2015-11-26 20:36:25 +08:00
|
|
|
FPM->add(createJumpThreadingPass());
|
|
|
|
FPM->add(createCorrelatedValuePropagationPass());
|
|
|
|
FPM->add(createDeadStoreEliminationPass());
|
|
|
|
FPM->add(createLICMPass());
|
|
|
|
FPM->add(createAggressiveDCEPass());
|
|
|
|
FPM->add(createCFGSimplificationPass());
|
2017-09-10 05:43:49 +08:00
|
|
|
FPM->add(createInstructionCombiningPass(true));
|
|
|
|
FPM->add(createFloat2IntPass());
|
2015-11-26 20:36:25 +08:00
|
|
|
|
|
|
|
return FPM->doInitialization();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool doFinalization(Module &M) override {
|
|
|
|
bool Result = FPM->doFinalization();
|
|
|
|
|
|
|
|
delete FPM;
|
|
|
|
FPM = nullptr;
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool runOnFunction(llvm::Function &F) override {
|
|
|
|
if (!F.hasFnAttribute("polly-optimized")) {
|
2018-05-15 21:37:17 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << F.getName()
|
|
|
|
<< ": Skipping cleanup because Polly did not optimize it.");
|
2015-11-26 20:36:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-15 21:37:17 +08:00
|
|
|
LLVM_DEBUG(dbgs() << F.getName() << ": Running codegen cleanup...");
|
2015-11-26 20:36:25 +08:00
|
|
|
return FPM->run(F);
|
|
|
|
}
|
|
|
|
//@}
|
|
|
|
};
|
|
|
|
|
|
|
|
char CodegenCleanup::ID;
|
2016-06-24 06:17:27 +08:00
|
|
|
} // namespace
|
2015-11-26 20:36:25 +08:00
|
|
|
|
|
|
|
FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup",
|
|
|
|
"Polly - Cleanup after code generation", false, false)
|
|
|
|
INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup",
|
|
|
|
"Polly - Cleanup after code generation", false, false)
|