diff --git a/llvm/include/llvm/Transforms/Scalar/Scalarizer.h b/llvm/include/llvm/Transforms/Scalar/Scalarizer.h --- a/llvm/include/llvm/Transforms/Scalar/Scalarizer.h +++ b/llvm/include/llvm/Transforms/Scalar/Scalarizer.h @@ -17,6 +17,7 @@ #ifndef LLVM_TRANSFORMS_SCALAR_SCALARIZER_H #define LLVM_TRANSFORMS_SCALAR_SCALARIZER_H +#include "llvm/ADT/Optional.h" #include "llvm/IR/PassManager.h" namespace llvm { @@ -24,9 +25,25 @@ class Function; class FunctionPass; +struct ScalarizerPassOptions { + // These optional booleans correspond 1:1 to cl::opt options defined in + // Scalarizer.cpp. When the cl::opt are specified, they take precedence. + // When the cl::opt are not specified, the present optional booleans allow to + // override the cl::opt's default values. + llvm::Optional ScalarizeVariableInsertExtract; + llvm::Optional ScalarizeLoadStore; +}; + class ScalarizerPass : public PassInfoMixin { + ScalarizerPassOptions Options; + public: PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); + + void setScalarizeVariableInsertExtract(bool Value) { + Options.ScalarizeVariableInsertExtract = Value; + } + void setScalarizeLoadStore(bool Value) { Options.ScalarizeLoadStore = Value; } }; /// Create a legacy pass manager instance of the Scalarizer pass diff --git a/llvm/lib/Transforms/Scalar/Scalarizer.cpp b/llvm/lib/Transforms/Scalar/Scalarizer.cpp --- a/llvm/lib/Transforms/Scalar/Scalarizer.cpp +++ b/llvm/lib/Transforms/Scalar/Scalarizer.cpp @@ -50,7 +50,7 @@ #define DEBUG_TYPE "scalarizer" -static cl::opt ScalarizeVariableInsertExtract( +static cl::opt ClScalarizeVariableInsertExtract( "scalarize-variable-insert-extract", cl::init(true), cl::Hidden, cl::desc("Allow the scalarizer pass to scalarize " "insertelement/extractelement with variable index")); @@ -58,9 +58,9 @@ // This is disabled by default because having separate loads and stores // makes it more likely that the -combiner-alias-analysis limits will be // reached. -static cl::opt - ScalarizeLoadStore("scalarize-load-store", cl::init(false), cl::Hidden, - cl::desc("Allow the scalarizer pass to scalarize loads and store")); +static cl::opt ClScalarizeLoadStore( + "scalarize-load-store", cl::init(false), cl::Hidden, + cl::desc("Allow the scalarizer pass to scalarize loads and store")); namespace { @@ -186,10 +186,23 @@ uint64_t ElemSize = 0; }; +template +T getWithDefaultOverride(const cl::opt &ClOption, + const llvm::Optional &DefaultOverride) { + return ClOption.getNumOccurrences() ? ClOption + : DefaultOverride.getValueOr(ClOption); +} + class ScalarizerVisitor : public InstVisitor { public: - ScalarizerVisitor(unsigned ParallelLoopAccessMDKind, DominatorTree *DT) - : ParallelLoopAccessMDKind(ParallelLoopAccessMDKind), DT(DT) { + ScalarizerVisitor(unsigned ParallelLoopAccessMDKind, DominatorTree *DT, + ScalarizerPassOptions Options) + : ParallelLoopAccessMDKind(ParallelLoopAccessMDKind), DT(DT), + ScalarizeVariableInsertExtract( + getWithDefaultOverride(ClScalarizeVariableInsertExtract, + Options.ScalarizeVariableInsertExtract)), + ScalarizeLoadStore(getWithDefaultOverride(ClScalarizeLoadStore, + Options.ScalarizeLoadStore)) { } bool visit(Function &F); @@ -235,6 +248,9 @@ unsigned ParallelLoopAccessMDKind; DominatorTree *DT; + + const bool ScalarizeVariableInsertExtract; + const bool ScalarizeLoadStore; }; class ScalarizerLegacyPass : public FunctionPass { @@ -334,7 +350,7 @@ unsigned ParallelLoopAccessMDKind = M.getContext().getMDKindID("llvm.mem.parallel_loop_access"); DominatorTree *DT = &getAnalysis().getDomTree(); - ScalarizerVisitor Impl(ParallelLoopAccessMDKind, DT); + ScalarizerVisitor Impl(ParallelLoopAccessMDKind, DT, ScalarizerPassOptions()); return Impl.visit(F); } @@ -983,7 +999,7 @@ unsigned ParallelLoopAccessMDKind = M.getContext().getMDKindID("llvm.mem.parallel_loop_access"); DominatorTree *DT = &AM.getResult(F); - ScalarizerVisitor Impl(ParallelLoopAccessMDKind, DT); + ScalarizerVisitor Impl(ParallelLoopAccessMDKind, DT, Options); bool Changed = Impl.visit(F); PreservedAnalyses PA; PA.preserve();