109 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// Run the set of default canonicalization passes.10//11// This pass is mainly used for debugging.12//13//===----------------------------------------------------------------------===//14 15#include "polly/Canonicalization.h"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::buildInlinerPipeline42static ModuleInlinerWrapperPass43buildInlinePasses(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 within48 // the CGSCC pipeline.49 MIWP.addModulePass(RequireAnalysisPass<GlobalsAA, Module>());50 // Invalidate AAManager so it can be recreated and pick up the newly available51 // GlobalsAA.52 MIWP.addModulePass(53 createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>()));54 55 // Require the ProfileSummaryAnalysis for the module so we can query it within56 // 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 pass61 // manager and trying to emulate its precise behavior. Much of this doesn't62 // 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 71FunctionPassManager72polly::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}109