|
12 | 12 | #include "BinaryPasses.h"
|
13 | 13 | #include "Passes/ReorderAlgorithm.h"
|
14 | 14 | #include "llvm/Support/Options.h"
|
| 15 | +#include <numeric> |
15 | 16 |
|
16 | 17 | #define DEBUG_TYPE "bolt"
|
17 | 18 |
|
@@ -178,6 +179,32 @@ TSPThreshold("tsp-threshold",
|
178 | 179 | cl::Hidden,
|
179 | 180 | cl::cat(BoltOptCategory));
|
180 | 181 |
|
| 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 | + |
181 | 208 | } // namespace opts
|
182 | 209 |
|
183 | 210 | namespace llvm {
|
@@ -986,13 +1013,27 @@ void Peepholes::removeUselessCondBranches(BinaryContext &BC,
|
986 | 1013 | void Peepholes::runOnFunctions(BinaryContext &BC,
|
987 | 1014 | std::map<uint64_t, BinaryFunction> &BFs,
|
988 | 1015 | 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 | + |
989 | 1026 | for (auto &It : BFs) {
|
990 | 1027 | auto &Function = It.second;
|
991 | 1028 | 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); |
996 | 1037 | }
|
997 | 1038 | }
|
998 | 1039 | outs() << "BOLT-INFO: Peephole: " << NumDoubleJumps
|
|
0 commit comments