<div dir="ltr">LGTM<div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Oct 5, 2015 at 1:59 PM Justin Bogner <<a href="mailto:mail@justinbogner.com">mail@justinbogner.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">ping.<br>
<br>
Justin Bogner <<a href="mailto:mail@justinbogner.com" target="_blank">mail@justinbogner.com</a>> writes:<br>
> The attached patch ports ADCE to the new pass manager. It's basically<br>
> mechanical and based off of some of the ports you've already done so<br>
> it's probably good to go in, but I have a couple of questions about<br>
> parts that seemed a little odd.<br>
><br>
> 1. The legacy pass states it preserves GlobalsAAWrapperPass, but the new<br>
>    pass returns PreservedAnalyses::none when it does work. This seems<br>
>    inconsistent, but maybe I'm missing something.<br>
><br>
> 2. Similarly, some of the other passes you've ported do<br>
>    AU.setPreservesCFG(), but this doesn't seem to be reflected in the<br>
>    new passes. How's this going to work?<br>
><br>
> 3. As per my reply to "Port SimplifyCFG to the new pass manager", it<br>
>    seems like we don't have good testing infrastructure in terms of<br>
>    preserved analyses. Do you have any ideas for how we're going to deal<br>
>    with that?<br>
><br>
> Anyway, let me know if this is good to land. I might tackle a couple<br>
> more of these in the near future, and some clarity on the above<br>
> questions would help.<br>
><br>
> commit cef9c843c206159c77d5ccccd2dba3914daa4452<br>
> Author: Justin Bogner <<a href="mailto:mail@justinbogner.com" target="_blank">mail@justinbogner.com</a>><br>
> Date:   Fri Sep 25 15:35:25 2015 -0700<br>
><br>
>     [PM] Port ADCE to the new pass manager<br>
><br>
> diff --git a/include/llvm/InitializePasses.h b/include/llvm/InitializePasses.h<br>
> index 75f9857..f888c97 100644<br>
> --- a/include/llvm/InitializePasses.h<br>
> +++ b/include/llvm/InitializePasses.h<br>
> @@ -61,7 +61,7 @@ void initializeTarget(PassRegistry&);<br>
><br>
>  void initializeAAEvalPass(PassRegistry&);<br>
>  void initializeAddDiscriminatorsPass(PassRegistry&);<br>
> -void initializeADCEPass(PassRegistry&);<br>
> +void initializeADCELegacyPassPass(PassRegistry&);<br>
>  void initializeBDCEPass(PassRegistry&);<br>
>  void initializeAliasSetPrinterPass(PassRegistry&);<br>
>  void initializeAlwaysInlinerPass(PassRegistry&);<br>
> diff --git a/include/llvm/Transforms/Scalar/ADCE.h b/include/llvm/Transforms/Scalar/ADCE.h<br>
> new file mode 100644<br>
> index 0000000..f9bc7b7<br>
> --- /dev/null<br>
> +++ b/include/llvm/Transforms/Scalar/ADCE.h<br>
> @@ -0,0 +1,38 @@<br>
> +//===- ADCE.h - Aggressive dead code elimination --------------------------===//<br>
> +//<br>
> +//                     The LLVM Compiler Infrastructure<br>
> +//<br>
> +// This file is distributed under the University of Illinois Open Source<br>
> +// License. See LICENSE.TXT for details.<br>
> +//<br>
> +//===----------------------------------------------------------------------===//<br>
> +//<br>
> +// This file provides the interface for the Aggressive Dead Code Elimination<br>
> +// pass. This pass optimistically assumes that all instructions are dead until<br>
> +// proven otherwise, allowing it to eliminate dead computations that other DCE<br>
> +// passes do not catch, particularly involving loop computations.<br>
> +//<br>
> +//===----------------------------------------------------------------------===//<br>
> +<br>
> +#ifndef LLVM_TRANSFORMS_SCALAR_ADCE_H<br>
> +#define LLVM_TRANSFORMS_SCALAR_ADCE_H<br>
> +<br>
> +#include "llvm/IR/Function.h"<br>
> +#include "llvm/IR/PassManager.h"<br>
> +<br>
> +namespace llvm {<br>
> +<br>
> +/// A DCE pass that assumes instructions are dead until proven otherwise.<br>
> +///<br>
> +/// This pass eliminates dead code by optimistically assuming that all<br>
> +/// instructions are dead until proven otherwise. This allows it to eliminate<br>
> +/// dead computations that other DCE passes do not catch, particularly involving<br>
> +/// loop computations.<br>
> +class ADCEPass {<br>
> +public:<br>
> +  static StringRef name() { return "ADCEPass"; }<br>
> +  PreservedAnalyses run(Function &F);<br>
> +};<br>
> +}<br>
> +<br>
> +#endif // LLVM_TRANSFORMS_SCALAR_ADCE_H<br>
> diff --git a/lib/Passes/PassBuilder.cpp b/lib/Passes/PassBuilder.cpp<br>
> index b8f6e20..f18fcb1 100644<br>
> --- a/lib/Passes/PassBuilder.cpp<br>
> +++ b/lib/Passes/PassBuilder.cpp<br>
> @@ -30,6 +30,7 @@<br>
>  #include "llvm/Support/Debug.h"<br>
>  #include "llvm/Target/TargetMachine.h"<br>
>  #include "llvm/Transforms/InstCombine/InstCombine.h"<br>
> +#include "llvm/Transforms/Scalar/ADCE.h"<br>
>  #include "llvm/Transforms/Scalar/EarlyCSE.h"<br>
>  #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"<br>
>  #include "llvm/Transforms/Scalar/SimplifyCFG.h"<br>
> diff --git a/lib/Passes/PassRegistry.def b/lib/Passes/PassRegistry.def<br>
> index da4e63b..6f52330 100644<br>
> --- a/lib/Passes/PassRegistry.def<br>
> +++ b/lib/Passes/PassRegistry.def<br>
> @@ -63,6 +63,7 @@ FUNCTION_ANALYSIS("targetir",<br>
>  #ifndef FUNCTION_PASS<br>
>  #define FUNCTION_PASS(NAME, CREATE_PASS)<br>
>  #endif<br>
> +FUNCTION_PASS("adce", ADCEPass())<br>
>  FUNCTION_PASS("early-cse", EarlyCSEPass())<br>
>  FUNCTION_PASS("instcombine", InstCombinePass())<br>
>  FUNCTION_PASS("invalidate<all>", InvalidateAllAnalysesPass())<br>
> diff --git a/lib/Transforms/Scalar/ADCE.cpp b/lib/Transforms/Scalar/ADCE.cpp<br>
> index c73766c..590a52d 100644<br>
> --- a/lib/Transforms/Scalar/ADCE.cpp<br>
> +++ b/lib/Transforms/Scalar/ADCE.cpp<br>
> @@ -14,7 +14,7 @@<br>
>  //<br>
>  //===----------------------------------------------------------------------===//<br>
><br>
> -#include "llvm/Transforms/Scalar.h"<br>
> +#include "llvm/Transforms/Scalar/ADCE.h"<br>
>  #include "llvm/ADT/DepthFirstIterator.h"<br>
>  #include "llvm/ADT/SmallPtrSet.h"<br>
>  #include "llvm/ADT/SmallVector.h"<br>
> @@ -26,35 +26,14 @@<br>
>  #include "llvm/IR/Instructions.h"<br>
>  #include "llvm/IR/IntrinsicInst.h"<br>
>  #include "llvm/Pass.h"<br>
> +#include "llvm/Transforms/Scalar.h"<br>
>  using namespace llvm;<br>
><br>
>  #define DEBUG_TYPE "adce"<br>
><br>
>  STATISTIC(NumRemoved, "Number of instructions removed");<br>
><br>
> -namespace {<br>
> -struct ADCE : public FunctionPass {<br>
> -  static char ID; // Pass identification, replacement for typeid<br>
> -  ADCE() : FunctionPass(ID) {<br>
> -    initializeADCEPass(*PassRegistry::getPassRegistry());<br>
> -  }<br>
> -<br>
> -  bool runOnFunction(Function& F) override;<br>
> -<br>
> -  void getAnalysisUsage(AnalysisUsage& AU) const override {<br>
> -    AU.setPreservesCFG();<br>
> -    AU.addPreserved<GlobalsAAWrapperPass>();<br>
> -  }<br>
> -};<br>
> -}<br>
> -<br>
> -char ADCE::ID = 0;<br>
> -INITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false)<br>
> -<br>
> -bool ADCE::runOnFunction(Function& F) {<br>
> -  if (skipOptnoneFunction(F))<br>
> -    return false;<br>
> -<br>
> +static bool aggressiveDCE(Function& F) {<br>
>    SmallPtrSet<Instruction*, 128> Alive;<br>
>    SmallVector<Instruction*, 128> Worklist;<br>
><br>
> @@ -96,6 +75,34 @@ bool ADCE::runOnFunction(Function& F) {<br>
>    return !Worklist.empty();<br>
>  }<br>
><br>
> -FunctionPass *llvm::createAggressiveDCEPass() {<br>
> -  return new ADCE();<br>
> +PreservedAnalyses ADCEPass::run(Function &F) {<br>
> +  if (aggressiveDCE(F))<br>
> +    return PreservedAnalyses::none();<br>
> +  return PreservedAnalyses::all();<br>
>  }<br>
> +<br>
> +namespace {<br>
> +struct ADCELegacyPass : public FunctionPass {<br>
> +  static char ID; // Pass identification, replacement for typeid<br>
> +  ADCELegacyPass() : FunctionPass(ID) {<br>
> +    initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());<br>
> +  }<br>
> +<br>
> +  bool runOnFunction(Function& F) override {<br>
> +    if (skipOptnoneFunction(F))<br>
> +      return false;<br>
> +    return aggressiveDCE(F);<br>
> +  }<br>
> +<br>
> +  void getAnalysisUsage(AnalysisUsage& AU) const override {<br>
> +    AU.setPreservesCFG();<br>
> +    AU.addPreserved<GlobalsAAWrapperPass>();<br>
> +  }<br>
> +};<br>
> +}<br>
> +<br>
> +char ADCELegacyPass::ID = 0;<br>
> +INITIALIZE_PASS(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination",<br>
> +                false, false)<br>
> +<br>
> +FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); }<br>
> diff --git a/lib/Transforms/Scalar/Scalar.cpp b/lib/Transforms/Scalar/Scalar.cpp<br>
> index f7a6134..fd74fa8 100644<br>
> --- a/lib/Transforms/Scalar/Scalar.cpp<br>
> +++ b/lib/Transforms/Scalar/Scalar.cpp<br>
> @@ -30,7 +30,7 @@ using namespace llvm;<br>
>  /// initializeScalarOptsPasses - Initialize all passes linked into the<br>
>  /// ScalarOpts library.<br>
>  void llvm::initializeScalarOpts(PassRegistry &Registry) {<br>
> -  initializeADCEPass(Registry);<br>
> +  initializeADCELegacyPassPass(Registry);<br>
>    initializeBDCEPass(Registry);<br>
>    initializeAlignmentFromAssumptionsPass(Registry);<br>
>    initializeConstantHoistingPass(Registry);<br>
> diff --git a/test/Transforms/ADCE/basictest.ll b/test/Transforms/ADCE/basictest.ll<br>
> index 61c4224..aaacc18 100644<br>
> --- a/test/Transforms/ADCE/basictest.ll<br>
> +++ b/test/Transforms/ADCE/basictest.ll<br>
> @@ -1,4 +1,5 @@<br>
>  ; RUN: opt < %s -adce -simplifycfg | llvm-dis<br>
> +; RUN: opt < %s -passes=adce | llvm-dis<br>
><br>
>  define i32 @Test(i32 %A, i32 %B) {<br>
>  BB1:<br>
</blockquote></div>