Skip to content

Commit

Permalink
Basic block clustering algorithm for minimizing branches.
Browse files Browse the repository at this point in the history
Summary:
This algorithm is similar to our main clustering algorithm but uses
a different heuristic for selecting edges to become fall-throughs.
The weight of an edge is calculated as the win in branches if we choose
to layout this edge as a fall-through. For example, the edges A -> B with
execution count 100 and A -> C with execution count 500 (where B and C
are the only successors of A) have weights -400 and +400 respectively.

(cherry picked from FBD3606591)
  • Loading branch information
Theodoros Kasampalis authored and maksfb committed Jul 15, 2016
1 parent a9bb332 commit ab599fe
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 57 deletions.
9 changes: 7 additions & 2 deletions bolt/BinaryFunction.cpp
Expand Up @@ -1181,7 +1181,8 @@ bool BinaryFunction::fixCFIState() {
return true;
}

void BinaryFunction::modifyLayout(LayoutType Type, bool Split) {
void BinaryFunction::modifyLayout(LayoutType Type, bool MinBranchClusters,
bool Split) {
if (BasicBlocksLayout.empty() || Type == LT_NONE)
return;

Expand All @@ -1203,7 +1204,11 @@ void BinaryFunction::modifyLayout(LayoutType Type, bool Split) {
else {
DEBUG(dbgs() << "running block layout heuristics on " << getName() << "\n");

std::unique_ptr<ClusterAlgorithm> CAlgo(new GreedyClusterAlgorithm());
std::unique_ptr<ClusterAlgorithm> CAlgo;
if (MinBranchClusters)
CAlgo.reset(new MinBranchGreedyClusterAlgorithm());
else
CAlgo.reset(new PHGreedyClusterAlgorithm());

switch(Type) {
case LT_OPTIMIZE:
Expand Down
2 changes: 1 addition & 1 deletion bolt/BinaryFunction.h
Expand Up @@ -424,7 +424,7 @@ class BinaryFunction : public AddressRangesOwner {

/// Modify code layout making necessary adjustments to instructions at the
/// end of basic blocks.
void modifyLayout(LayoutType Type, bool Split);
void modifyLayout(LayoutType Type, bool MinBranchClusters, bool Split);

/// Find the loops in the CFG of the function and store infromation about
/// them.
Expand Down
10 changes: 9 additions & 1 deletion bolt/BinaryPasses.cpp
Expand Up @@ -52,6 +52,13 @@ ReorderBlocks(
"behavior"),
clEnumValEnd));

static llvm::cl::opt<bool>
MinBranchClusters(
"min-branch-clusters",
llvm::cl::desc("use a modified clustering algorithm geared towards "
"minimizing branches"),
llvm::cl::Hidden);

} // namespace opts

namespace llvm {
Expand Down Expand Up @@ -384,7 +391,8 @@ void ReorderBasicBlocks::runOnFunctions(
(opts::SplitFunctions == BinaryFunction::ST_EH &&
Function.hasEHRanges()) ||
(LargeFunctions.find(It.first) != LargeFunctions.end());
Function.modifyLayout(opts::ReorderBlocks, ShouldSplit);
Function.modifyLayout(opts::ReorderBlocks, opts::MinBranchClusters,
ShouldSplit);
if (opts::PrintAll || opts::PrintReordered)
Function.print(errs(), "after reordering blocks", true);
if (opts::DumpDotAll)
Expand Down

0 comments on commit ab599fe

Please sign in to comment.