Skip to content

Commit 5ebdfed

Browse files
Bill Nellmemfrob
authored andcommitted
[BOLT] Add finer control of peephole pass.
Summary: Add selective control over peephole options. This makes it easier to test which ones might have a positive effect. (cherry picked from FBD6289659)
1 parent 268ead1 commit 5ebdfed

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

bolt/BinaryPassManager.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ OptimizeBodylessFunctions("optimize-bodyless-functions",
7676
cl::ZeroOrMore,
7777
cl::cat(BoltOptCategory));
7878

79-
static cl::opt<bool>
80-
Peepholes("peepholes",
81-
cl::desc("run peephole optimizations"),
82-
cl::ZeroOrMore,
83-
cl::cat(BoltOptCategory));
84-
8579
static cl::opt<bool>
8680
PrintAfterBranchFixup("print-after-branch-fixup",
8781
cl::desc("print function after fixing local branches"),
@@ -332,8 +326,7 @@ void BinaryFunctionPassManager::runAllPasses(
332326

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

335-
Manager.registerPass(llvm::make_unique<Peepholes>(PrintPeepholes),
336-
opts::Peepholes);
329+
Manager.registerPass(llvm::make_unique<Peepholes>(PrintPeepholes));
337330

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

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

356-
Manager.registerPass(llvm::make_unique<Peepholes>(PrintPeepholes),
357-
opts::Peepholes);
349+
Manager.registerPass(llvm::make_unique<Peepholes>(PrintPeepholes));
358350

359351
Manager.registerPass(
360352
llvm::make_unique<EliminateUnreachableBlocks>(PrintUCE),

bolt/Passes/BinaryPasses.cpp

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "BinaryPasses.h"
1313
#include "Passes/ReorderAlgorithm.h"
1414
#include "llvm/Support/Options.h"
15+
#include <numeric>
1516

1617
#define DEBUG_TYPE "bolt"
1718

@@ -178,6 +179,32 @@ TSPThreshold("tsp-threshold",
178179
cl::Hidden,
179180
cl::cat(BoltOptCategory));
180181

182+
enum PeepholeOpts : char {
183+
PEEP_NONE = 0x0,
184+
PEEP_SHORTEN = 0x1,
185+
PEEP_DOUBLE_JUMPS = 0x2,
186+
PEEP_TAILCALL_TRAPS = 0x4,
187+
PEEP_USELESS_BRANCHES = 0x8,
188+
PEEP_ALL = 0xf
189+
};
190+
191+
static cl::list<PeepholeOpts>
192+
Peepholes("peepholes",
193+
cl::CommaSeparated,
194+
cl::desc("enable peephole optimizations"),
195+
cl::value_desc("opt1,opt2,opt3,..."),
196+
cl::values(
197+
clEnumValN(PEEP_SHORTEN, "shorten", "perform instruction shortening"),
198+
clEnumValN(PEEP_DOUBLE_JUMPS, "double-jumps",
199+
"remove double jumps when able"),
200+
clEnumValN(PEEP_TAILCALL_TRAPS, "tailcall-traps", "insert tail call traps"),
201+
clEnumValN(PEEP_USELESS_BRANCHES, "useless-branches",
202+
"remove useless conditional branches"),
203+
clEnumValN(PEEP_ALL, "all", "enable all peephole optimizations"),
204+
clEnumValEnd),
205+
cl::ZeroOrMore,
206+
cl::cat(BoltOptCategory));
207+
181208
} // namespace opts
182209

183210
namespace llvm {
@@ -986,13 +1013,27 @@ void Peepholes::removeUselessCondBranches(BinaryContext &BC,
9861013
void Peepholes::runOnFunctions(BinaryContext &BC,
9871014
std::map<uint64_t, BinaryFunction> &BFs,
9881015
std::set<uint64_t> &LargeFunctions) {
1016+
const char Opts =
1017+
std::accumulate(opts::Peepholes.begin(),
1018+
opts::Peepholes.end(),
1019+
0,
1020+
[](const char A, const opts::PeepholeOpts B) {
1021+
return A | B;
1022+
});
1023+
if (Opts == opts::PEEP_NONE)
1024+
return;
1025+
9891026
for (auto &It : BFs) {
9901027
auto &Function = It.second;
9911028
if (shouldOptimize(Function)) {
992-
shortenInstructions(BC, Function);
993-
NumDoubleJumps += fixDoubleJumps(BC, Function, false);
994-
addTailcallTraps(BC, Function);
995-
removeUselessCondBranches(BC, Function);
1029+
if (Opts & opts::PEEP_SHORTEN)
1030+
shortenInstructions(BC, Function);
1031+
if (Opts & opts::PEEP_DOUBLE_JUMPS)
1032+
NumDoubleJumps += fixDoubleJumps(BC, Function, false);
1033+
if (Opts & opts::PEEP_TAILCALL_TRAPS)
1034+
addTailcallTraps(BC, Function);
1035+
if (Opts & opts::PEEP_USELESS_BRANCHES)
1036+
removeUselessCondBranches(BC, Function);
9961037
}
9971038
}
9981039
outs() << "BOLT-INFO: Peephole: " << NumDoubleJumps

0 commit comments

Comments
 (0)