Skip to content

Commit

Permalink
[BOLT] Add finer control of peephole pass.
Browse files Browse the repository at this point in the history
Summary: Add selective control over peephole options.  This makes it easier to test which ones might have a positive effect.

(cherry picked from FBD6289659)
  • Loading branch information
Bill Nell authored and memfrob committed Oct 4, 2022
1 parent 268ead1 commit 5ebdfed
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
12 changes: 2 additions & 10 deletions bolt/BinaryPassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ OptimizeBodylessFunctions("optimize-bodyless-functions",
cl::ZeroOrMore,
cl::cat(BoltOptCategory));

static cl::opt<bool>
Peepholes("peepholes",
cl::desc("run peephole optimizations"),
cl::ZeroOrMore,
cl::cat(BoltOptCategory));

static cl::opt<bool>
PrintAfterBranchFixup("print-after-branch-fixup",
cl::desc("print function after fixing local branches"),
Expand Down Expand Up @@ -332,8 +326,7 @@ void BinaryFunctionPassManager::runAllPasses(

Manager.registerPass(llvm::make_unique<IndirectCallPromotion>(PrintICP));

Manager.registerPass(llvm::make_unique<Peepholes>(PrintPeepholes),
opts::Peepholes);
Manager.registerPass(llvm::make_unique<Peepholes>(PrintPeepholes));

Manager.registerPass(llvm::make_unique<InlineSmallFunctions>(PrintInline),
opts::InlineSmallFunctions);
Expand All @@ -353,8 +346,7 @@ void BinaryFunctionPassManager::runAllPasses(

Manager.registerPass(llvm::make_unique<ReorderBasicBlocks>(PrintReordered));

Manager.registerPass(llvm::make_unique<Peepholes>(PrintPeepholes),
opts::Peepholes);
Manager.registerPass(llvm::make_unique<Peepholes>(PrintPeepholes));

Manager.registerPass(
llvm::make_unique<EliminateUnreachableBlocks>(PrintUCE),
Expand Down
49 changes: 45 additions & 4 deletions bolt/Passes/BinaryPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "BinaryPasses.h"
#include "Passes/ReorderAlgorithm.h"
#include "llvm/Support/Options.h"
#include <numeric>

#define DEBUG_TYPE "bolt"

Expand Down Expand Up @@ -178,6 +179,32 @@ TSPThreshold("tsp-threshold",
cl::Hidden,
cl::cat(BoltOptCategory));

enum PeepholeOpts : char {
PEEP_NONE = 0x0,
PEEP_SHORTEN = 0x1,
PEEP_DOUBLE_JUMPS = 0x2,
PEEP_TAILCALL_TRAPS = 0x4,
PEEP_USELESS_BRANCHES = 0x8,
PEEP_ALL = 0xf
};

static cl::list<PeepholeOpts>
Peepholes("peepholes",
cl::CommaSeparated,
cl::desc("enable peephole optimizations"),
cl::value_desc("opt1,opt2,opt3,..."),
cl::values(
clEnumValN(PEEP_SHORTEN, "shorten", "perform instruction shortening"),
clEnumValN(PEEP_DOUBLE_JUMPS, "double-jumps",
"remove double jumps when able"),
clEnumValN(PEEP_TAILCALL_TRAPS, "tailcall-traps", "insert tail call traps"),
clEnumValN(PEEP_USELESS_BRANCHES, "useless-branches",
"remove useless conditional branches"),
clEnumValN(PEEP_ALL, "all", "enable all peephole optimizations"),
clEnumValEnd),
cl::ZeroOrMore,
cl::cat(BoltOptCategory));

} // namespace opts

namespace llvm {
Expand Down Expand Up @@ -986,13 +1013,27 @@ void Peepholes::removeUselessCondBranches(BinaryContext &BC,
void Peepholes::runOnFunctions(BinaryContext &BC,
std::map<uint64_t, BinaryFunction> &BFs,
std::set<uint64_t> &LargeFunctions) {
const char Opts =
std::accumulate(opts::Peepholes.begin(),
opts::Peepholes.end(),
0,
[](const char A, const opts::PeepholeOpts B) {
return A | B;
});
if (Opts == opts::PEEP_NONE)
return;

for (auto &It : BFs) {
auto &Function = It.second;
if (shouldOptimize(Function)) {
shortenInstructions(BC, Function);
NumDoubleJumps += fixDoubleJumps(BC, Function, false);
addTailcallTraps(BC, Function);
removeUselessCondBranches(BC, Function);
if (Opts & opts::PEEP_SHORTEN)
shortenInstructions(BC, Function);
if (Opts & opts::PEEP_DOUBLE_JUMPS)
NumDoubleJumps += fixDoubleJumps(BC, Function, false);
if (Opts & opts::PEEP_TAILCALL_TRAPS)
addTailcallTraps(BC, Function);
if (Opts & opts::PEEP_USELESS_BRANCHES)
removeUselessCondBranches(BC, Function);
}
}
outs() << "BOLT-INFO: Peephole: " << NumDoubleJumps
Expand Down

0 comments on commit 5ebdfed

Please sign in to comment.