[llvm] r227726 - [PM] Port SimplifyCFG to the new pass manager.

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 25 14:39:57 PDT 2015


Chandler Carruth <chandlerc at gmail.com> writes:
> Author: chandlerc
> Date: Sun Feb  1 05:34:21 2015
> New Revision: 227726
>
> URL: http://llvm.org/viewvc/llvm-project?rev=227726&view=rev
> Log:
> [PM] Port SimplifyCFG to the new pass manager.
>
> This should be sufficient to replace the initial (minor) function pass
> pipeline in Clang with the new pass manager. I'll probably add an (off
> by default) flag to do that just to ensure we can get extra testing.
>
> Added:
>     llvm/trunk/include/llvm/Transforms/Scalar/SimplifyCFG.h
> Modified:
>     llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp
>     llvm/trunk/test/Transforms/SimplifyCFG/basictest.ll
>     llvm/trunk/tools/opt/PassRegistry.def
>     llvm/trunk/tools/opt/Passes.cpp
>
> Added: llvm/trunk/include/llvm/Transforms/Scalar/SimplifyCFG.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar/SimplifyCFG.h?rev=227726&view=auto
> ==============================================================================
> --- llvm/trunk/include/llvm/Transforms/Scalar/SimplifyCFG.h (added)
> +++ llvm/trunk/include/llvm/Transforms/Scalar/SimplifyCFG.h Sun Feb  1 05:34:21 2015
> @@ -0,0 +1,46 @@
> +//===- SimplifyCFG.h - Simplify and canonicalize the CFG --------*- C++ -*-===//
> +//
> +//                     The LLVM Compiler Infrastructure
> +//
> +// This file is distributed under the University of Illinois Open Source
> +// License. See LICENSE.TXT for details.
> +//
> +//===----------------------------------------------------------------------===//
> +/// \file
> +/// This file provides the interface for the pass responsible for both
> +/// simplifying and canonicalizing the CFG.
> +///
> +//===----------------------------------------------------------------------===//
> +
> +#ifndef LLVM_TRANSFORMS_SCALAR_SIMPLIFYCFG_H
> +#define LLVM_TRANSFORMS_SCALAR_SIMPLIFYCFG_H
> +
> +#include "llvm/IR/Function.h"
> +#include "llvm/IR/PassManager.h"
> +
> +namespace llvm {
> +
> +/// \brief A pass to simplify and canonicalize the CFG of a function.
> +///
> +/// This pass iteratively simplifies the entire CFG of a function, removing
> +/// unnecessary control flows and bringing it into the canonical form expected
> +/// by the rest of the mid-level optimizer.
> +class SimplifyCFGPass {
> +  int BonusInstThreshold;
> +
> +public:
> +  static StringRef name() { return "SimplifyCFGPass"; }
> +
> +  /// \brief Construct a pass with the default thresholds.
> +  SimplifyCFGPass();
> +
> +  /// \brief Construct a pass with a specific bonus threshold.
> +  SimplifyCFGPass(int BonusInstThreshold);
> +
> +  /// \brief Run the pass over the function.
> +  PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
> +};
> +
> +}
> +
> +#endif
>
> Modified: llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp?rev=227726&r1=227725&r2=227726&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp (original)
> +++ llvm/trunk/lib/Transforms/Scalar/SimplifyCFGPass.cpp Sun Feb  1 05:34:21 2015
> @@ -21,7 +21,7 @@
>  //
>  //===----------------------------------------------------------------------===//
>  
> -#include "llvm/Transforms/Scalar.h"
> +#include "llvm/Transforms/Scalar/SimplifyCFG.h"
>  #include "llvm/ADT/SmallPtrSet.h"
>  #include "llvm/ADT/SmallVector.h"
>  #include "llvm/ADT/Statistic.h"
> @@ -37,6 +37,7 @@
>  #include "llvm/Pass.h"
>  #include "llvm/Support/CommandLine.h"
>  #include "llvm/Transforms/Utils/Local.h"
> +#include "llvm/Transforms/Scalar.h"
>  using namespace llvm;
>  
>  #define DEBUG_TYPE "simplifycfg"
> @@ -47,36 +48,6 @@ UserBonusInstThreshold("bonus-inst-thres
>  
>  STATISTIC(NumSimpl, "Number of blocks simplified");
>  
> -namespace {
> -struct CFGSimplifyPass : public FunctionPass {
> -  static char ID; // Pass identification, replacement for typeid
> -  unsigned BonusInstThreshold;
> -  CFGSimplifyPass(int T = -1) : FunctionPass(ID) {
> -    BonusInstThreshold = (T == -1) ? UserBonusInstThreshold : unsigned(T);
> -    initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
> -  }
> -  bool runOnFunction(Function &F) override;
> -
> -  void getAnalysisUsage(AnalysisUsage &AU) const override {
> -    AU.addRequired<AssumptionCacheTracker>();
> -    AU.addRequired<TargetTransformInfoWrapperPass>();
> -  }
> -};
> -}
> -
> -char CFGSimplifyPass::ID = 0;
> -INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
> -                      false)
> -INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
> -INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
> -INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
> -                    false)
> -
> -// Public interface to the CFGSimplification pass
> -FunctionPass *llvm::createCFGSimplificationPass(int Threshold) {
> -  return new CFGSimplifyPass(Threshold);
> -}
> -
>  /// mergeEmptyReturnBlocks - If we have more than one empty (other than phi
>  /// node) return blocks, merge them together to promote recursive block merging.
>  static bool mergeEmptyReturnBlocks(Function &F) {
> @@ -176,19 +147,9 @@ static bool iterativelySimplifyCFG(Funct
>    return Changed;
>  }
>  
> -// It is possible that we may require multiple passes over the code to fully
> -// simplify the CFG.
> -//
> -bool CFGSimplifyPass::runOnFunction(Function &F) {
> -  if (skipOptnoneFunction(F))
> -    return false;
> -
> -  AssumptionCache *AC =
> -      &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
> -  const TargetTransformInfo &TTI =
> -      getAnalysis<TargetTransformInfoWrapperPass>().getTTI();
> -  DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
> -  const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
> +static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI,
> +                                const DataLayout *DL, AssumptionCache *AC,
> +                                int BonusInstThreshold) {
>    bool EverChanged = removeUnreachableBlocks(F);
>    EverChanged |= mergeEmptyReturnBlocks(F);
>    EverChanged |= iterativelySimplifyCFG(F, TTI, DL, AC, BonusInstThreshold);
> @@ -211,3 +172,63 @@ bool CFGSimplifyPass::runOnFunction(Func
>  
>    return true;
>  }
> +
> +SimplifyCFGPass::SimplifyCFGPass()
> +    : BonusInstThreshold(UserBonusInstThreshold) {}
> +
> +SimplifyCFGPass::SimplifyCFGPass(int BonusInstThreshold)
> +    : BonusInstThreshold(BonusInstThreshold) {}
> +
> +PreservedAnalyses SimplifyCFGPass::run(Function &F,
> +                                       AnalysisManager<Function> *AM) {
> +  auto *DL = F.getParent()->getDataLayout();
> +  auto &TTI = AM->getResult<TargetIRAnalysis>(F);
> +  auto &AC = AM->getResult<AssumptionAnalysis>(F);
> +
> +  if (!simplifyFunctionCFG(F, TTI, DL, &AC, BonusInstThreshold))
> +    return PreservedAnalyses::none();

Uh, isn't this check *exactly backwards*? simplifyFunctionCFG() returns
true for "did work" and false for "did nothing"...

> +
> +  return PreservedAnalyses::all();
> +}
> +
> +namespace {
> +struct CFGSimplifyPass : public FunctionPass {
> +  static char ID; // Pass identification, replacement for typeid
> +  unsigned BonusInstThreshold;
> +  CFGSimplifyPass(int T = -1) : FunctionPass(ID) {
> +    BonusInstThreshold = (T == -1) ? UserBonusInstThreshold : unsigned(T);
> +    initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
> +  }
> +  bool runOnFunction(Function &F) override {
> +    if (skipOptnoneFunction(F))
> +      return false;
> +
> +    AssumptionCache *AC =
> +        &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
> +    const TargetTransformInfo &TTI =
> +        getAnalysis<TargetTransformInfoWrapperPass>().getTTI();
> +    DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
> +    const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
> +    return simplifyFunctionCFG(F, TTI, DL, AC, BonusInstThreshold);
> +  }
> +
> +  void getAnalysisUsage(AnalysisUsage &AU) const override {
> +    AU.addRequired<AssumptionCacheTracker>();
> +    AU.addRequired<TargetTransformInfoWrapperPass>();
> +  }
> +};
> +}
> +
> +char CFGSimplifyPass::ID = 0;
> +INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
> +                      false)
> +INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
> +INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
> +INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
> +                    false)
> +
> +// Public interface to the CFGSimplification pass
> +FunctionPass *llvm::createCFGSimplificationPass(int Threshold) {
> +  return new CFGSimplifyPass(Threshold);
> +}
> +
>
> Modified: llvm/trunk/test/Transforms/SimplifyCFG/basictest.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/basictest.ll?rev=227726&r1=227725&r2=227726&view=diff
> ==============================================================================
> --- llvm/trunk/test/Transforms/SimplifyCFG/basictest.ll (original)
> +++ llvm/trunk/test/Transforms/SimplifyCFG/basictest.ll Sun Feb  1 05:34:21 2015
> @@ -1,6 +1,7 @@
>  ; Test CFG simplify removal of branch instructions.
>  ;
>  ; RUN: opt < %s -simplifycfg -S | FileCheck %s
> +; RUN: opt < %s -passes=simplify-cfg -S | FileCheck %s
>  
>  define void @test1() {
>          br label %1
>
> Modified: llvm/trunk/tools/opt/PassRegistry.def
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/PassRegistry.def?rev=227726&r1=227725&r2=227726&view=diff
> ==============================================================================
> --- llvm/trunk/tools/opt/PassRegistry.def (original)
> +++ llvm/trunk/tools/opt/PassRegistry.def Sun Feb  1 05:34:21 2015
> @@ -71,6 +71,7 @@ FUNCTION_PASS("print", PrintFunctionPass
>  FUNCTION_PASS("print<assumptions>", AssumptionPrinterPass(dbgs()))
>  FUNCTION_PASS("print<domtree>", DominatorTreePrinterPass(dbgs()))
>  FUNCTION_PASS("print<loops>", LoopPrinterPass(dbgs()))
> +FUNCTION_PASS("simplify-cfg", SimplifyCFGPass())
>  FUNCTION_PASS("verify", VerifierPass())
>  FUNCTION_PASS("verify<domtree>", DominatorTreeVerifierPass())
>  #undef FUNCTION_PASS
>
> Modified: llvm/trunk/tools/opt/Passes.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/Passes.cpp?rev=227726&r1=227725&r2=227726&view=diff
> ==============================================================================
> --- llvm/trunk/tools/opt/Passes.cpp (original)
> +++ llvm/trunk/tools/opt/Passes.cpp Sun Feb  1 05:34:21 2015
> @@ -30,6 +30,7 @@
>  #include "llvm/Transforms/InstCombine/InstCombine.h"
>  #include "llvm/Transforms/Scalar/EarlyCSE.h"
>  #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
> +#include "llvm/Transforms/Scalar/SimplifyCFG.h"
>  
>  using namespace llvm;
>  
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits


More information about the llvm-commits mailing list