Skip to content

Commit

Permalink
[BOLT] Enable SCTC by default.
Browse files Browse the repository at this point in the history
(cherry picked from FBD4837849)
  • Loading branch information
Bill Nell authored and maksfb committed Apr 5, 2017
1 parent 34c8a7c commit c7cccac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
7 changes: 6 additions & 1 deletion bolt/BinaryFunction.cpp
Expand Up @@ -3889,7 +3889,12 @@ DynoStats BinaryFunction::getDynoStats() const {
NonTakenCount = BBExecutionCount - TakenCount;
else
NonTakenCount = 0;
IsForwardBranch = isForwardBranch(BB, BB->getFallthrough());

// If succ_size == 0 then we are branching to a function
// rather than a BB label.
IsForwardBranch = BB->succ_size() == 0
? isForwardCall(BC.MIA->getTargetSymbol(*CondBranch))
: isForwardBranch(BB, BB->getFallthrough());
}

if (TakenCount == COUNT_NO_PROFILE)
Expand Down
1 change: 1 addition & 0 deletions bolt/BinaryPassManager.cpp
Expand Up @@ -176,6 +176,7 @@ PrintUCE("print-uce",
static cl::opt<bool>
SimplifyConditionalTailCalls("simplify-conditional-tail-calls",
cl::desc("simplify conditional tail calls by removing unnecessary jumps"),
cl::init(true),
cl::ZeroOrMore,
cl::cat(BoltOptCategory));

Expand Down
26 changes: 17 additions & 9 deletions bolt/Passes/BinaryPasses.cpp
Expand Up @@ -230,7 +230,7 @@ enum SctcModes : char {
static cl::opt<SctcModes>
SctcMode("sctc-mode",
cl::desc("mode for simplify conditional tail calls"),
cl::init(SctcHeuristic),
cl::init(SctcAlways),
cl::values(clEnumValN(SctcAlways, "always", "always perform sctc"),
clEnumValN(SctcPreserveDirection,
"preserve",
Expand Down Expand Up @@ -475,7 +475,9 @@ namespace {
// B0: ...
// jmp B2 (or jcc B2)
//
uint64_t fixDoubleJumps(BinaryContext &BC, BinaryFunction &Function) {
uint64_t fixDoubleJumps(BinaryContext &BC,
BinaryFunction &Function,
bool MarkInvalid) {
uint64_t NumDoubleJumps = 0;

for (auto &BB : Function) {
Expand All @@ -484,7 +486,7 @@ uint64_t fixDoubleJumps(BinaryContext &BC, BinaryFunction &Function) {
const MCSymbol *SuccSym) {
// Ignore infinite loop jumps or fallthrough tail jumps.
if (Pred == Succ || Succ == &BB)
return;
return false;

if (Succ) {
const MCSymbol *TBB = nullptr;
Expand All @@ -495,7 +497,7 @@ uint64_t fixDoubleJumps(BinaryContext &BC, BinaryFunction &Function) {
if(!Res) {
DEBUG(dbgs() << "analyzeBranch failed in peepholes in block:\n";
Pred->dump());
return;
return false;
}
Pred->replaceSuccessor(&BB, Succ);

Expand All @@ -519,7 +521,7 @@ uint64_t fixDoubleJumps(BinaryContext &BC, BinaryFunction &Function) {
Pred->eraseInstruction(Branch);
Pred->addTailCallInstruction(SuccSym);
} else {
return;
return false;
}
}

Expand All @@ -528,6 +530,8 @@ uint64_t fixDoubleJumps(BinaryContext &BC, BinaryFunction &Function) {
<< Pred->getName() << " -> " << BB.getName() << " to "
<< Pred->getName() << " -> " << SuccSym->getName()
<< (!Succ ? " (tail)\n" : "\n"));

return true;
};

if (BB.getNumNonPseudos() != 1 || BB.isLandingPad())
Expand All @@ -542,7 +546,7 @@ uint64_t fixDoubleJumps(BinaryContext &BC, BinaryFunction &Function) {
const auto *SuccSym = BC.MIA->getTargetSymbol(*Inst);
auto *Succ = BB.getSuccessor();

if ((!Succ || &BB == Succ) && !IsTailCall)
if (((!Succ || &BB == Succ) && !IsTailCall) || (IsTailCall && !SuccSym))
continue;

std::vector<BinaryBasicBlock *> Preds{BB.pred_begin(), BB.pred_end()};
Expand All @@ -554,7 +558,11 @@ uint64_t fixDoubleJumps(BinaryContext &BC, BinaryFunction &Function) {
if (Pred->getSuccessor() == &BB ||
(Pred->getConditionalSuccessor(true) == &BB && !IsTailCall) ||
Pred->getConditionalSuccessor(false) == &BB) {
checkAndPatch(Pred, Succ, SuccSym);
if (checkAndPatch(Pred, Succ, SuccSym) && MarkInvalid) {
BB.markValid(BB.pred_size() != 0 ||
BB.isLandingPad() ||
BB.isEntryPoint());
}
assert(Function.validateCFG());
}
}
Expand Down Expand Up @@ -698,7 +706,7 @@ uint64_t SimplifyConditionalTailCalls::fixTailCalls(BinaryContext &BC,
}

if (NumLocalCTCs > 0) {
NumDoubleJumps += fixDoubleJumps(BC, BF);
NumDoubleJumps += fixDoubleJumps(BC, BF, true);
// Clean-up unreachable tail-call blocks.
const auto Stats = BF.eraseInvalidBBs();
DeletedBlocks += Stats.first;
Expand Down Expand Up @@ -798,7 +806,7 @@ void Peepholes::runOnFunctions(BinaryContext &BC,
auto &Function = It.second;
if (shouldOptimize(Function)) {
shortenInstructions(BC, Function);
NumDoubleJumps += fixDoubleJumps(BC, Function);
NumDoubleJumps += fixDoubleJumps(BC, Function, false);
addTailcallTraps(BC, Function);
removeUselessCondBranches(BC, Function);
}
Expand Down

0 comments on commit c7cccac

Please sign in to comment.