diff --git a/llvm/include/llvm/Transforms/Scalar.h b/llvm/include/llvm/Transforms/Scalar.h index a1aacec769794a..7f5583570a44f3 100644 --- a/llvm/include/llvm/Transforms/Scalar.h +++ b/llvm/include/llvm/Transforms/Scalar.h @@ -14,6 +14,7 @@ #ifndef LLVM_TRANSFORMS_SCALAR_H #define LLVM_TRANSFORMS_SCALAR_H +#include "llvm/Transforms/Utils/SimplifyCFGOptions.h" #include namespace llvm { @@ -256,8 +257,7 @@ FunctionPass *createJumpThreadingPass(int Threshold = -1); // simplify terminator instructions, convert switches to lookup tables, etc. // FunctionPass *createCFGSimplificationPass( - unsigned Threshold = 1, bool ForwardSwitchCond = false, - bool ConvertSwitch = false, bool KeepLoops = true, bool SinkCommon = false, + SimplifyCFGOptions Options = SimplifyCFGOptions(), std::function Ftor = nullptr); //===----------------------------------------------------------------------===// diff --git a/llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h b/llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h index f9792d38bbe6bf..3625eb176b830b 100644 --- a/llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h +++ b/llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h @@ -14,9 +14,9 @@ #ifndef LLVM_TRANSFORMS_SCALAR_SIMPLIFYCFG_H #define LLVM_TRANSFORMS_SCALAR_SIMPLIFYCFG_H -#include "llvm/Transforms/Utils/Local.h" #include "llvm/IR/Function.h" #include "llvm/IR/PassManager.h" +#include "llvm/Transforms/Utils/SimplifyCFGOptions.h" namespace llvm { diff --git a/llvm/include/llvm/Transforms/Utils/Local.h b/llvm/include/llvm/Transforms/Utils/Local.h index 3fab3bc21a0785..5cc8d1fa743760 100644 --- a/llvm/include/llvm/Transforms/Utils/Local.h +++ b/llvm/include/llvm/Transforms/Utils/Local.h @@ -30,6 +30,7 @@ #include "llvm/IR/Value.h" #include "llvm/IR/ValueHandle.h" #include "llvm/Support/Casting.h" +#include "llvm/Transforms/Utils/SimplifyCFGOptions.h" #include #include @@ -58,73 +59,6 @@ class StoreInst; class TargetLibraryInfo; class TargetTransformInfo; -/// A set of parameters used to control the transforms in the SimplifyCFG pass. -/// Options may change depending on the position in the optimization pipeline. -/// For example, canonical form that includes switches and branches may later be -/// replaced by lookup tables and selects. -struct SimplifyCFGOptions { - int BonusInstThreshold; - bool ForwardSwitchCondToPhi; - bool ConvertSwitchToLookupTable; - bool NeedCanonicalLoop; - bool SinkCommonInsts; - bool SimplifyCondBranch; - bool FoldTwoEntryPHINode; - - AssumptionCache *AC; - - SimplifyCFGOptions(unsigned BonusThreshold = 1, - bool ForwardSwitchCond = false, - bool SwitchToLookup = false, bool CanonicalLoops = true, - bool SinkCommon = false, - AssumptionCache *AssumpCache = nullptr, - bool SimplifyCondBranch = true, - bool FoldTwoEntryPHINode = true) - : BonusInstThreshold(BonusThreshold), - ForwardSwitchCondToPhi(ForwardSwitchCond), - ConvertSwitchToLookupTable(SwitchToLookup), - NeedCanonicalLoop(CanonicalLoops), - SinkCommonInsts(SinkCommon), - SimplifyCondBranch(SimplifyCondBranch), - FoldTwoEntryPHINode(FoldTwoEntryPHINode), - AC(AssumpCache) {} - - // Support 'builder' pattern to set members by name at construction time. - SimplifyCFGOptions &bonusInstThreshold(int I) { - BonusInstThreshold = I; - return *this; - } - SimplifyCFGOptions &forwardSwitchCondToPhi(bool B) { - ForwardSwitchCondToPhi = B; - return *this; - } - SimplifyCFGOptions &convertSwitchToLookupTable(bool B) { - ConvertSwitchToLookupTable = B; - return *this; - } - SimplifyCFGOptions &needCanonicalLoops(bool B) { - NeedCanonicalLoop = B; - return *this; - } - SimplifyCFGOptions &sinkCommonInsts(bool B) { - SinkCommonInsts = B; - return *this; - } - SimplifyCFGOptions &setAssumptionCache(AssumptionCache *Cache) { - AC = Cache; - return *this; - } - SimplifyCFGOptions &setSimplifyCondBranch(bool B) { - SimplifyCondBranch = B; - return *this; - } - - SimplifyCFGOptions &setFoldTwoEntryPHINode(bool B) { - FoldTwoEntryPHINode = B; - return *this; - } -}; - //===----------------------------------------------------------------------===// // Local constant propagation. // diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h b/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h new file mode 100644 index 00000000000000..75cd2582f9981d --- /dev/null +++ b/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h @@ -0,0 +1,86 @@ +//===- SimplifyCFGOptions.h - Control structure for SimplifyCFG -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// A set of parameters used to control the transforms in the SimplifyCFG pass. +// Options may change depending on the position in the optimization pipeline. +// For example, canonical form that includes switches and branches may later be +// replaced by lookup tables and selects. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYCFGOPTIONS_H +#define LLVM_TRANSFORMS_UTILS_SIMPLIFYCFGOPTIONS_H + +namespace llvm { + +class AssumptionCache; + +struct SimplifyCFGOptions { + int BonusInstThreshold; + bool ForwardSwitchCondToPhi; + bool ConvertSwitchToLookupTable; + bool NeedCanonicalLoop; + bool SinkCommonInsts; + bool SimplifyCondBranch; + bool FoldTwoEntryPHINode; + + AssumptionCache *AC; + + SimplifyCFGOptions(unsigned BonusThreshold = 1, + bool ForwardSwitchCond = false, + bool SwitchToLookup = false, bool CanonicalLoops = true, + bool SinkCommon = false, + AssumptionCache *AssumpCache = nullptr, + bool SimplifyCondBranch = true, + bool FoldTwoEntryPHINode = true) + : BonusInstThreshold(BonusThreshold), + ForwardSwitchCondToPhi(ForwardSwitchCond), + ConvertSwitchToLookupTable(SwitchToLookup), + NeedCanonicalLoop(CanonicalLoops), SinkCommonInsts(SinkCommon), + SimplifyCondBranch(SimplifyCondBranch), + FoldTwoEntryPHINode(FoldTwoEntryPHINode), AC(AssumpCache) {} + + // Support 'builder' pattern to set members by name at construction time. + SimplifyCFGOptions &bonusInstThreshold(int I) { + BonusInstThreshold = I; + return *this; + } + SimplifyCFGOptions &forwardSwitchCondToPhi(bool B) { + ForwardSwitchCondToPhi = B; + return *this; + } + SimplifyCFGOptions &convertSwitchToLookupTable(bool B) { + ConvertSwitchToLookupTable = B; + return *this; + } + SimplifyCFGOptions &needCanonicalLoops(bool B) { + NeedCanonicalLoop = B; + return *this; + } + SimplifyCFGOptions &sinkCommonInsts(bool B) { + SinkCommonInsts = B; + return *this; + } + SimplifyCFGOptions &setAssumptionCache(AssumptionCache *Cache) { + AC = Cache; + return *this; + } + SimplifyCFGOptions &setSimplifyCondBranch(bool B) { + SimplifyCondBranch = B; + return *this; + } + + SimplifyCFGOptions &setFoldTwoEntryPHINode(bool B) { + FoldTwoEntryPHINode = B; + return *this; + } +}; + +} // namespace llvm + +#endif // LLVM_TRANSFORMS_UTILS_SIMPLIFYCFGOPTIONS_H diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp index a63b9a97ada552..b0cef9b66e017c 100644 --- a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp +++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp @@ -453,7 +453,11 @@ void AArch64PassConfig::addIRPasses() { // determine whether it succeeded. We can exploit existing control-flow in // ldrex/strex loops to simplify this, but it needs tidying up. if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy) - addPass(createCFGSimplificationPass(1, true, true, false, true)); + addPass(createCFGSimplificationPass(SimplifyCFGOptions() + .forwardSwitchCondToPhi(true) + .convertSwitchToLookupTable(true) + .needCanonicalLoops(false) + .sinkCommonInsts(true))); // Run LoopDataPrefetch // diff --git a/llvm/lib/Target/ARM/ARMTargetMachine.cpp b/llvm/lib/Target/ARM/ARMTargetMachine.cpp index 9ead5fa4308c34..b316b1041f2c55 100644 --- a/llvm/lib/Target/ARM/ARMTargetMachine.cpp +++ b/llvm/lib/Target/ARM/ARMTargetMachine.cpp @@ -409,7 +409,7 @@ void ARMPassConfig::addIRPasses() { // ldrex/strex loops to simplify this, but it needs tidying up. if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy) addPass(createCFGSimplificationPass( - 1, false, false, true, true, [this](const Function &F) { + SimplifyCFGOptions().sinkCommonInsts(true), [this](const Function &F) { const auto &ST = this->TM->getSubtarget(F); return ST.hasAnyDataBarrier() && !ST.isThumb1Only(); })); diff --git a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp index 3fe42ea13f51ba..49d98622d946cc 100644 --- a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp +++ b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp @@ -320,7 +320,11 @@ void HexagonPassConfig::addIRPasses() { if (!NoOpt) { if (EnableInitialCFGCleanup) - addPass(createCFGSimplificationPass(1, true, true, false, true)); + addPass(createCFGSimplificationPass(SimplifyCFGOptions() + .forwardSwitchCondToPhi(true) + .convertSwitchToLookupTable(true) + .needCanonicalLoops(false) + .sinkCommonInsts(true))); if (EnableLoopPrefetch) addPass(createLoopDataPrefetchPass()); if (EnableCommGEP) diff --git a/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp b/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp index 24814370f57aa8..f5ba86b04ed068 100644 --- a/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp +++ b/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp @@ -784,7 +784,11 @@ void PassManagerBuilder::populateModulePassManager( // convert to more optimized IR using more aggressive simplify CFG options. // The extra sinking transform can create larger basic blocks, so do this // before SLP vectorization. - MPM.add(createCFGSimplificationPass(1, true, true, false, true)); + MPM.add(createCFGSimplificationPass(SimplifyCFGOptions() + .forwardSwitchCondToPhi(true) + .convertSwitchToLookupTable(true) + .needCanonicalLoops(false) + .sinkCommonInsts(true))); if (SLPVectorize) { MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. diff --git a/llvm/lib/Transforms/Scalar/Scalar.cpp b/llvm/lib/Transforms/Scalar/Scalar.cpp index 9d088547b4369c..42f79d89f0a28c 100644 --- a/llvm/lib/Transforms/Scalar/Scalar.cpp +++ b/llvm/lib/Transforms/Scalar/Scalar.cpp @@ -139,7 +139,7 @@ void LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM) { } void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM) { - unwrap(PM)->add(createCFGSimplificationPass(1, false, false, true)); + unwrap(PM)->add(createCFGSimplificationPass()); } void LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM) { diff --git a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp index 4187d5b55adf4e..d48d5408dd3c39 100644 --- a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp +++ b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp @@ -40,6 +40,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar/SimplifyCFG.h" #include "llvm/Transforms/Utils/Local.h" +#include "llvm/Transforms/Utils/SimplifyCFGOptions.h" #include using namespace llvm; @@ -304,15 +305,7 @@ INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false, // Public interface to the CFGSimplification pass FunctionPass * -llvm::createCFGSimplificationPass(unsigned Threshold, bool ForwardSwitchCond, - bool ConvertSwitch, bool KeepLoops, - bool SinkCommon, +llvm::createCFGSimplificationPass(SimplifyCFGOptions Options, std::function Ftor) { - return new CFGSimplifyPass(SimplifyCFGOptions() - .bonusInstThreshold(Threshold) - .forwardSwitchCondToPhi(ForwardSwitchCond) - .convertSwitchToLookupTable(ConvertSwitch) - .needCanonicalLoops(KeepLoops) - .sinkCommonInsts(SinkCommon), - std::move(Ftor)); + return new CFGSimplifyPass(Options, std::move(Ftor)); }