From b025d375a154a9faf1d27f0d281fd92c91d34a65 Mon Sep 17 00:00:00 2001 From: Sean Silva Date: Wed, 6 Jul 2016 23:26:29 +0000 Subject: [PATCH] [PM] Port CorrelatedValuePropagation llvm-svn: 274705 --- .../Scalar/CorrelatedValuePropagation.h | 24 +++++++++++++ llvm/lib/Passes/PassBuilder.cpp | 1 + llvm/lib/Passes/PassRegistry.def | 1 + .../Scalar/CorrelatedValuePropagation.cpp | 34 +++++++++++++++---- .../CorrelatedValuePropagation/icmp.ll | 1 + 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 llvm/include/llvm/Transforms/Scalar/CorrelatedValuePropagation.h diff --git a/llvm/include/llvm/Transforms/Scalar/CorrelatedValuePropagation.h b/llvm/include/llvm/Transforms/Scalar/CorrelatedValuePropagation.h new file mode 100644 index 000000000000..38816bbed068 --- /dev/null +++ b/llvm/include/llvm/Transforms/Scalar/CorrelatedValuePropagation.h @@ -0,0 +1,24 @@ +//===---- CorrelatedValuePropagation.h --------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_SCALAR_CORRELATEDVALUEPROPAGATION_H +#define LLVM_TRANSFORMS_SCALAR_CORRELATEDVALUEPROPAGATION_H + +#include "llvm/IR/Function.h" +#include "llvm/IR/PassManager.h" + +namespace llvm { + +struct CorrelatedValuePropagationPass + : PassInfoMixin { + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; +} + +#endif // LLVM_TRANSFORMS_SCALAR_CORRELATEDVALUEPROPAGATION_H diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index 5ebe98361d0c..421b6ab53aed 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -75,6 +75,7 @@ #include "llvm/Transforms/Scalar/ADCE.h" #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h" #include "llvm/Transforms/Scalar/BDCE.h" +#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h" #include "llvm/Transforms/Scalar/DCE.h" #include "llvm/Transforms/Scalar/ConstantHoisting.h" #include "llvm/Transforms/Scalar/DeadStoreElimination.h" diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def index 9190844951b8..71307a960bc2 100644 --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -127,6 +127,7 @@ FUNCTION_PASS("add-discriminators", AddDiscriminatorsPass()) FUNCTION_PASS("alignment-from-assumptions", AlignmentFromAssumptionsPass()) FUNCTION_PASS("bdce", BDCEPass()) FUNCTION_PASS("consthoist", ConstantHoistingPass()) +FUNCTION_PASS("correlated-propagation", CorrelatedValuePropagationPass()) FUNCTION_PASS("dce", DCEPass()) FUNCTION_PASS("dse", DSEPass()) FUNCTION_PASS("early-cse", EarlyCSEPass()) diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp index b7944a345794..c09f3534dcfb 100644 --- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp +++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp @@ -11,6 +11,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h" #include "llvm/Transforms/Scalar.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/GlobalsModRef.h" @@ -39,7 +40,6 @@ STATISTIC(NumSDivs, "Number of sdiv converted to udiv"); namespace { class CorrelatedValuePropagation : public FunctionPass { - LazyValueInfo *LVI; public: static char ID; CorrelatedValuePropagation(): FunctionPass(ID) { @@ -384,12 +384,7 @@ static Constant *getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI) { ConstantInt::getFalse(C->getContext()); } -bool CorrelatedValuePropagation::runOnFunction(Function &F) { - if (skipFunction(F)) - return false; - - LVI = &getAnalysis().getLVI(); - +static bool runImpl(Function &F, LazyValueInfo *LVI) { bool FnChanged = false; for (BasicBlock &BB : F) { @@ -447,3 +442,28 @@ bool CorrelatedValuePropagation::runOnFunction(Function &F) { return FnChanged; } + +bool CorrelatedValuePropagation::runOnFunction(Function &F) { + if (skipFunction(F)) + return false; + + LazyValueInfo *LVI = &getAnalysis().getLVI(); + return runImpl(F, LVI); +} + +PreservedAnalyses +CorrelatedValuePropagationPass::run(Function &F, FunctionAnalysisManager &AM) { + + LazyValueInfo *LVI = &AM.getResult(F); + bool Changed = runImpl(F, LVI); + + // FIXME: We need to invalidate LVI to avoid PR28400. Is there a better + // solution? + AM.invalidate(F); + + if (!Changed) + return PreservedAnalyses::all(); + PreservedAnalyses PA; + PA.preserve(); + return PA; +} diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll index c2863ffda0fb..9e525a39dad5 100644 --- a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll +++ b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll @@ -1,4 +1,5 @@ ; RUN: opt -correlated-propagation -S %s | FileCheck %s +; RUN: opt -passes=correlated-propagation -S %s | FileCheck %s target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.10.0"