[SimplifyCFG] use pass options and remove the latesimplifycfg pass

This is no-functional-change-intended.

This is repackaging the functionality of D30333 (defer switch-to-lookup-tables) and 
D35411 (defer folding unconditional branches) with pass parameters rather than a named
"latesimplifycfg" pass. Now that we have individual options to control the functionality,
we could decouple when these fire (but that's an independent patch if desired). 

The next planned step would be to add another option bit to disable the sinking transform
mentioned in D38566. This should also make it clear that the new pass manager needs to
be updated to limit simplifycfg in the same way as the old pass manager.

Differential Revision: https://reviews.llvm.org/D38631

llvm-svn: 316835
This commit is contained in:
Sanjay Patel
2017-10-28 18:43:07 +00:00
parent 25808c303f
commit b049173157
25 changed files with 186 additions and 136 deletions

View File

@@ -625,7 +625,9 @@ void PassManagerBuilder::populateModulePassManager(
}
addExtensionsToPM(EP_Peephole, MPM);
MPM.add(createLateCFGSimplificationPass()); // Switches to lookup tables
// Switches to lookup tables and other transforms that may not be considered
// canonical by other IR passes.
MPM.add(createCFGSimplificationPass(1, true, true, false));
addInstructionCombiningPass(MPM);
if (!DisableUnrollLoops) {

View File

@@ -85,7 +85,6 @@ void llvm::initializeScalarOpts(PassRegistry &Registry) {
initializeIPSCCPLegacyPassPass(Registry);
initializeSROALegacyPassPass(Registry);
initializeCFGSimplifyPassPass(Registry);
initializeLateCFGSimplifyPassPass(Registry);
initializeStructurizeCFGPass(Registry);
initializeSimpleLoopUnswitchLegacyPassPass(Registry);
initializeSinkingLegacyPassPass(Registry);
@@ -119,11 +118,7 @@ void LLVMAddAlignmentFromAssumptionsPass(LLVMPassManagerRef PM) {
}
void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM) {
unwrap(PM)->add(createCFGSimplificationPass());
}
void LLVMAddLateCFGSimplificationPass(LLVMPassManagerRef PM) {
unwrap(PM)->add(createLateCFGSimplificationPass());
unwrap(PM)->add(createCFGSimplificationPass(1, false, false, true));
}
void LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM) {

View File

@@ -45,9 +45,21 @@ using namespace llvm;
#define DEBUG_TYPE "simplifycfg"
static cl::opt<unsigned>
UserBonusInstThreshold("bonus-inst-threshold", cl::Hidden, cl::init(1),
cl::desc("Control the number of bonus instructions (default = 1)"));
static cl::opt<unsigned> UserBonusInstThreshold(
"bonus-inst-threshold", cl::Hidden, cl::init(1),
cl::desc("Control the number of bonus instructions (default = 1)"));
static cl::opt<bool> UserKeepLoops(
"keep-loops", cl::Hidden, cl::init(true),
cl::desc("Preserve canonical loop structure (default = true)"));
static cl::opt<bool> UserSwitchToLookup(
"switch-to-lookup", cl::Hidden, cl::init(false),
cl::desc("Convert switches to lookup tables (default = false)"));
static cl::opt<bool> UserForwardSwitchCond(
"forward-switch-cond", cl::Hidden, cl::init(false),
cl::desc("Forward switch condition to phi ops (default = false)"));
STATISTIC(NumSimpl, "Number of blocks simplified");
@@ -179,13 +191,21 @@ static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI,
return true;
}
// FIXME: The new pass manager always creates a "late" simplifycfg pass using
// this default constructor.
SimplifyCFGPass::SimplifyCFGPass()
: Options(UserBonusInstThreshold, true, true, false) {}
SimplifyCFGPass::SimplifyCFGPass(const SimplifyCFGOptions &PassOptions)
: Options(PassOptions) {}
// Command-line settings override compile-time settings.
SimplifyCFGPass::SimplifyCFGPass(const SimplifyCFGOptions &Opts) {
Options.BonusInstThreshold = UserBonusInstThreshold.getNumOccurrences()
? UserBonusInstThreshold
: Opts.BonusInstThreshold;
Options.ForwardSwitchCondToPhi = UserForwardSwitchCond.getNumOccurrences()
? UserForwardSwitchCond
: Opts.ForwardSwitchCondToPhi;
Options.ConvertSwitchToLookupTable = UserSwitchToLookup.getNumOccurrences()
? UserSwitchToLookup
: Opts.ConvertSwitchToLookupTable;
Options.NeedCanonicalLoop = UserKeepLoops.getNumOccurrences()
? UserKeepLoops
: Opts.NeedCanonicalLoop;
}
PreservedAnalyses SimplifyCFGPass::run(Function &F,
FunctionAnalysisManager &AM) {
@@ -199,62 +219,49 @@ PreservedAnalyses SimplifyCFGPass::run(Function &F,
}
namespace {
struct BaseCFGSimplifyPass : public FunctionPass {
struct CFGSimplifyPass : public FunctionPass {
static char ID;
SimplifyCFGOptions Options;
std::function<bool(const Function &)> PredicateFtor;
int BonusInstThreshold;
bool ForwardSwitchCondToPhi;
bool ConvertSwitchToLookupTable;
bool KeepCanonicalLoops;
BaseCFGSimplifyPass(int T, bool ForwardSwitchCond, bool ConvertSwitch,
bool KeepLoops,
std::function<bool(const Function &)> Ftor, char &ID)
: FunctionPass(ID), PredicateFtor(std::move(Ftor)),
ForwardSwitchCondToPhi(ForwardSwitchCond),
ConvertSwitchToLookupTable(ConvertSwitch),
KeepCanonicalLoops(KeepLoops) {
BonusInstThreshold = (T == -1) ? UserBonusInstThreshold : T;
CFGSimplifyPass(unsigned Threshold = 1, bool ForwardSwitchCond = false,
bool ConvertSwitch = false, bool KeepLoops = true,
std::function<bool(const Function &)> Ftor = nullptr)
: FunctionPass(ID), PredicateFtor(std::move(Ftor)) {
initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
// Check for command-line overrides of options for debug/customization.
Options.BonusInstThreshold = UserBonusInstThreshold.getNumOccurrences()
? UserBonusInstThreshold
: Threshold;
Options.ForwardSwitchCondToPhi = UserForwardSwitchCond.getNumOccurrences()
? UserForwardSwitchCond
: ForwardSwitchCond;
Options.ConvertSwitchToLookupTable = UserSwitchToLookup.getNumOccurrences()
? UserSwitchToLookup
: ConvertSwitch;
Options.NeedCanonicalLoop =
UserKeepLoops.getNumOccurrences() ? UserKeepLoops : KeepLoops;
}
bool runOnFunction(Function &F) override {
if (skipFunction(F) || (PredicateFtor && !PredicateFtor(F)))
return false;
AssumptionCache *AC =
&getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
const TargetTransformInfo &TTI =
getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
return simplifyFunctionCFG(F, TTI,
{BonusInstThreshold, ForwardSwitchCondToPhi,
ConvertSwitchToLookupTable, KeepCanonicalLoops,
AC});
Options.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
return simplifyFunctionCFG(F, TTI, Options);
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<AssumptionCacheTracker>();
AU.addRequired<TargetTransformInfoWrapperPass>();
AU.addPreserved<GlobalsAAWrapperPass>();
}
};
struct CFGSimplifyPass : public BaseCFGSimplifyPass {
static char ID; // Pass identification, replacement for typeid
CFGSimplifyPass(int T = -1,
std::function<bool(const Function &)> Ftor = nullptr)
: BaseCFGSimplifyPass(T, false, false, true, Ftor, ID) {
initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
}
};
struct LateCFGSimplifyPass : public BaseCFGSimplifyPass {
static char ID; // Pass identification, replacement for typeid
LateCFGSimplifyPass(int T = -1,
std::function<bool(const Function &)> Ftor = nullptr)
: BaseCFGSimplifyPass(T, true, true, false, Ftor, ID) {
initializeLateCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
}
};
}
char CFGSimplifyPass::ID = 0;
@@ -265,24 +272,11 @@ INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
false)
char LateCFGSimplifyPass::ID = 0;
INITIALIZE_PASS_BEGIN(LateCFGSimplifyPass, "latesimplifycfg",
"Simplify the CFG more aggressively", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
INITIALIZE_PASS_END(LateCFGSimplifyPass, "latesimplifycfg",
"Simplify the CFG more aggressively", false, false)
// Public interface to the CFGSimplification pass
FunctionPass *
llvm::createCFGSimplificationPass(int Threshold,
std::function<bool(const Function &)> Ftor) {
return new CFGSimplifyPass(Threshold, std::move(Ftor));
}
// Public interface to the LateCFGSimplification pass
FunctionPass *
llvm::createLateCFGSimplificationPass(int Threshold,
llvm::createCFGSimplificationPass(unsigned Threshold, bool ForwardSwitchCond,
bool ConvertSwitch, bool KeepLoops,
std::function<bool(const Function &)> Ftor) {
return new LateCFGSimplifyPass(Threshold, std::move(Ftor));
return new CFGSimplifyPass(Threshold, ForwardSwitchCond, ConvertSwitch,
KeepLoops, std::move(Ftor));
}