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 @@ -22,9 +22,25 @@ namespace llvm { +struct ScalarizerPassOptions { + // For these optional booleans, the default value None lets the implementation + // pick a default value based on corresponding command-line options defined in + // Scalarizer.cpp. Setting a non-default value here allows to override that, + // which is useful when creating a ScalarizerPass from another pass. + 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 @@ -52,7 +52,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")); @@ -60,12 +60,22 @@ // 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 { +void overrideDefaultValuesWithCommandLineValues( + ScalarizerPassOptions &options) { + if (!options.scalarizeVariableInsertExtract.hasValue()) { + options.scalarizeVariableInsertExtract = clScalarizeVariableInsertExtract; + } + if (!options.scalarizeLoadStore.hasValue()) { + options.scalarizeLoadStore = clScalarizeLoadStore; + } +} + BasicBlock::iterator skipPastPhiNodesAndDbg(BasicBlock::iterator Itr) { BasicBlock *BB = Itr->getParent(); if (isa(Itr)) @@ -190,8 +200,11 @@ 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), + options(options) { + overrideDefaultValuesWithCommandLineValues(options); } bool visit(Function &F); @@ -237,6 +250,8 @@ unsigned ParallelLoopAccessMDKind; DominatorTree *DT; + + ScalarizerPassOptions options; }; class ScalarizerLegacyPass : public FunctionPass { @@ -334,7 +349,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); } @@ -780,7 +795,7 @@ for (unsigned I = 0; I < NumElems; ++I) Res[I] = CI->getValue().getZExtValue() == I ? NewElt : Op0[I]; } else { - if (!ScalarizeVariableInsertExtract) + if (!options.scalarizeVariableInsertExtract) return false; for (unsigned I = 0; I < NumElems; ++I) { @@ -813,7 +828,7 @@ return true; } - if (!ScalarizeVariableInsertExtract) + if (!options.scalarizeVariableInsertExtract) return false; Value *Res = UndefValue::get(VT->getElementType()); @@ -879,7 +894,7 @@ } bool ScalarizerVisitor::visitLoadInst(LoadInst &LI) { - if (!ScalarizeLoadStore) + if (!options.scalarizeLoadStore) return false; if (!LI.isSimple()) return false; @@ -904,7 +919,7 @@ } bool ScalarizerVisitor::visitStoreInst(StoreInst &SI) { - if (!ScalarizeLoadStore) + if (!options.scalarizeLoadStore) return false; if (!SI.isSimple()) return false; @@ -982,7 +997,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();