Polly 22.0.0git
Canonicalization.cpp
Go to the documentation of this file.
1//===---- Canonicalization.cpp - Run canonicalization passes --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Run the set of default canonicalization passes.
10//
11// This pass is mainly used for debugging.
12//
13//===----------------------------------------------------------------------===//
14
16#include "polly/Options.h"
17#include "llvm/Analysis/GlobalsModRef.h"
18#include "llvm/Analysis/ProfileSummaryInfo.h"
19#include "llvm/IR/LegacyPassManager.h"
20#include "llvm/Transforms/IPO.h"
21#include "llvm/Transforms/IPO/FunctionAttrs.h"
22#include "llvm/Transforms/InstCombine/InstCombine.h"
23#include "llvm/Transforms/Scalar.h"
24#include "llvm/Transforms/Scalar/EarlyCSE.h"
25#include "llvm/Transforms/Scalar/IndVarSimplify.h"
26#include "llvm/Transforms/Scalar/LoopRotation.h"
27#include "llvm/Transforms/Scalar/Reassociate.h"
28#include "llvm/Transforms/Scalar/SimplifyCFG.h"
29#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
30#include "llvm/Transforms/Utils.h"
31#include "llvm/Transforms/Utils/Mem2Reg.h"
32
33using namespace llvm;
34using namespace polly;
35
36static cl::opt<bool>
37 PollyInliner("polly-run-inliner",
38 cl::desc("Run an early inliner pass before Polly"), cl::Hidden,
39 cl::cat(PollyCategory));
40
41/// Adapted from llvm::PassBuilder::buildInlinerPipeline
42static ModuleInlinerWrapperPass
43buildInlinePasses(llvm::OptimizationLevel Level) {
44 InlineParams IP = getInlineParams(200);
45 ModuleInlinerWrapperPass MIWP(IP);
46
47 // Require the GlobalsAA analysis for the module so we can query it within
48 // the CGSCC pipeline.
49 MIWP.addModulePass(RequireAnalysisPass<GlobalsAA, Module>());
50 // Invalidate AAManager so it can be recreated and pick up the newly available
51 // GlobalsAA.
52 MIWP.addModulePass(
53 createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>()));
54
55 // Require the ProfileSummaryAnalysis for the module so we can query it within
56 // the inliner pass.
57 MIWP.addModulePass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
58
59 // Now begin the main postorder CGSCC pipeline.
60 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
61 // manager and trying to emulate its precise behavior. Much of this doesn't
62 // make a lot of sense and we should revisit the core CGSCC structure.
63 CGSCCPassManager &MainCGPipeline = MIWP.getPM();
64
65 // Now deduce any function attributes based in the current code.
66 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
67
68 return MIWP;
69}
70
71FunctionPassManager
72polly::buildCanonicalicationPassesForNPM(llvm::ModulePassManager &MPM,
73 llvm::OptimizationLevel Level) {
74 FunctionPassManager FPM;
75
76 bool UseMemSSA = true;
77 FPM.addPass(PromotePass());
78 FPM.addPass(EarlyCSEPass(UseMemSSA));
79 FPM.addPass(InstCombinePass());
80 FPM.addPass(SimplifyCFGPass());
81 FPM.addPass(TailCallElimPass());
82 FPM.addPass(SimplifyCFGPass());
83 FPM.addPass(ReassociatePass());
84 {
85 LoopPassManager LPM;
86 LPM.addPass(LoopRotatePass(Level != OptimizationLevel::Oz));
87 FPM.addPass(createFunctionToLoopPassAdaptor<LoopPassManager>(
88 std::move(LPM), /*UseMemorySSA=*/false));
89 }
90 if (PollyInliner) {
91 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
92 MPM.addPass(buildInlinePasses(Level));
93 FPM = FunctionPassManager();
94
95 FPM.addPass(PromotePass());
96 FPM.addPass(SimplifyCFGPass());
97 FPM.addPass(InstCombinePass());
98 }
99 FPM.addPass(InstCombinePass());
100 {
101 LoopPassManager LPM;
102 LPM.addPass(IndVarSimplifyPass());
103 FPM.addPass(createFunctionToLoopPassAdaptor<LoopPassManager>(
104 std::move(LPM), /*UseMemorySSA=*/false));
105 }
106
107 return FPM;
108}
static cl::opt< bool > PollyInliner("polly-run-inliner", cl::desc("Run an early inliner pass before Polly"), cl::Hidden, cl::cat(PollyCategory))
static ModuleInlinerWrapperPass buildInlinePasses(llvm::OptimizationLevel Level)
Adapted from llvm::PassBuilder::buildInlinerPipeline.
llvm::cl::OptionCategory PollyCategory
llvm::FunctionPassManager buildCanonicalicationPassesForNPM(llvm::ModulePassManager &MPM, llvm::OptimizationLevel Level)
Schedule a set of canonicalization passes to prepare for Polly.