Skip to content

Commit

Permalink
[BOLT] Fix bug in shortening peephole.
Browse files Browse the repository at this point in the history
Summary: The arithmetic shortening code on x86 was broken.  It would sometimes shorten instructions with immediate operands that wouldn't fit into 8 bits.

(cherry picked from FBD6444699)
  • Loading branch information
Bill Nell authored and maksfb committed Nov 30, 2017
1 parent 0bab742 commit a71b570
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
32 changes: 27 additions & 5 deletions bolt/Passes/BinaryPasses.cpp
Expand Up @@ -974,13 +974,33 @@ void SimplifyConditionalTailCalls::runOnFunctions(
<< ".\n";
}

void Peepholes::shortenInstructions(BinaryContext &BC,
BinaryFunction &Function) {
uint64_t Peepholes::shortenInstructions(BinaryContext &BC,
BinaryFunction &Function) {
std::string DebugStr;
(void)DebugStr;
uint64_t Count = 0;
for (auto &BB : Function) {
for (auto &Inst : BB) {
BC.MIA->shortenInstruction(Inst);
DEBUG(
if (opts::Verbosity > 1) {
DebugStr.clear();
raw_string_ostream OS(DebugStr);
BC.printInstruction(OS, Inst, 0, &Function);
OS.str();
});
if (BC.MIA->shortenInstruction(Inst)) {
DEBUG(
if (opts::Verbosity > 1) {
dbgs() << "BOLT-INFO: peephole, shortening:\n"
<< "BOLT-INFO: " << DebugStr
<< "BOLT-INFO: to:";
BC.printInstruction(dbgs(), Inst, 0, &Function);
});
++Count;
}
}
}
return Count;
}

void Peepholes::addTailcallTraps(BinaryContext &BC,
Expand Down Expand Up @@ -1041,7 +1061,7 @@ void Peepholes::runOnFunctions(BinaryContext &BC,
auto &Function = It.second;
if (shouldOptimize(Function)) {
if (Opts & opts::PEEP_SHORTEN)
shortenInstructions(BC, Function);
NumShortened += shortenInstructions(BC, Function);
if (Opts & opts::PEEP_DOUBLE_JUMPS)
NumDoubleJumps += fixDoubleJumps(BC, Function, false);
if (Opts & opts::PEEP_TAILCALL_TRAPS)
Expand All @@ -1050,7 +1070,9 @@ void Peepholes::runOnFunctions(BinaryContext &BC,
removeUselessCondBranches(BC, Function);
}
}
outs() << "BOLT-INFO: Peephole: " << NumDoubleJumps
outs() << "BOLT-INFO: Peephole: " << NumShortened
<< " instructions shortened.\n"
<< "BOLT-INFO: Peephole: " << NumDoubleJumps
<< " double jumps patched.\n"
<< "BOLT-INFO: Peephole: " << TailCallTraps
<< " tail call traps inserted.\n"
Expand Down
3 changes: 2 additions & 1 deletion bolt/Passes/BinaryPasses.h
Expand Up @@ -301,13 +301,14 @@ class SimplifyConditionalTailCalls : public BinaryFunctionPass {

/// Perform simple peephole optimizations.
class Peepholes : public BinaryFunctionPass {
uint64_t NumShortened{0};
uint64_t NumDoubleJumps{0};
uint64_t TailCallTraps{0};
uint64_t NumUselessCondBranches{0};

/// Attempt to use the minimum operand width for arithmetic, branch and
/// move instructions.
void shortenInstructions(BinaryContext &BC, BinaryFunction &Function);
uint64_t shortenInstructions(BinaryContext &BC, BinaryFunction &Function);

/// Add trap instructions immediately after indirect tail calls to prevent
/// the processor from decoding instructions immediate following the
Expand Down

0 comments on commit a71b570

Please sign in to comment.