Skip to content

Commit

Permalink
[BranchProbability] move options for 'likely' and 'unlikely'
Browse files Browse the repository at this point in the history
This makes the settings available for use in other passes by housing
them within the Support lib, but NFC otherwise.

See D98898 for the proposed usage in SimplifyCFG
(where this change was originally included).

Differential Revision: https://reviews.llvm.org/D98945
  • Loading branch information
rotateright committed Mar 20, 2021
1 parent 47fdaa3 commit ee8b538
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenFunction.cpp
Expand Up @@ -42,8 +42,8 @@
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/CRC.h"
#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
using namespace clang;
using namespace CodeGen;
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/Support/BranchProbability.h
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
#define LLVM_SUPPORT_BRANCHPROBABILITY_H

#include "llvm/Support/CommandLine.h"
#include "llvm/Support/DataTypes.h"
#include <algorithm>
#include <cassert>
Expand All @@ -21,6 +22,9 @@

namespace llvm {

extern cl::opt<uint32_t> LikelyBranchWeight;
extern cl::opt<uint32_t> UnlikelyBranchWeight;

class raw_ostream;

// This class represents Branch Probability as a non-negative fraction that is
Expand Down
3 changes: 0 additions & 3 deletions llvm/include/llvm/Transforms/Scalar/LowerExpectIntrinsic.h
Expand Up @@ -17,7 +17,6 @@

#include "llvm/IR/Function.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/CommandLine.h"

namespace llvm {

Expand All @@ -32,8 +31,6 @@ struct LowerExpectIntrinsicPass : PassInfoMixin<LowerExpectIntrinsicPass> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &);
};

extern cl::opt<uint32_t> LikelyBranchWeight;
extern cl::opt<uint32_t> UnlikelyBranchWeight;
}

#endif
14 changes: 14 additions & 0 deletions llvm/lib/Support/BranchProbability.cpp
Expand Up @@ -19,6 +19,20 @@

using namespace llvm;

// These default values are chosen to represent an extremely skewed outcome for
// a condition, but they leave some room for interpretation by later passes.
//
// If the documentation for __builtin_expect() was made explicit that it should
// only be used in extreme cases, we could make this ratio higher. As it stands,
// programmers may be using __builtin_expect() / llvm.expect to annotate that a
// branch is only mildly likely or unlikely to be taken.
cl::opt<uint32_t> llvm::LikelyBranchWeight(
"likely-branch-weight", cl::Hidden, cl::init(2000),
cl::desc("Weight of the branch likely to be taken (default = 2000)"));
cl::opt<uint32_t> llvm::UnlikelyBranchWeight(
"unlikely-branch-weight", cl::Hidden, cl::init(1),
cl::desc("Weight of the branch unlikely to be taken (default = 1)"));

constexpr uint32_t BranchProbability::D;

raw_ostream &BranchProbability::print(raw_ostream &OS) const {
Expand Down
20 changes: 1 addition & 19 deletions llvm/lib/Transforms/Scalar/LowerExpectIntrinsic.cpp
Expand Up @@ -24,6 +24,7 @@
#include "llvm/IR/Metadata.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/Scalar.h"

Expand All @@ -34,25 +35,6 @@ using namespace llvm;
STATISTIC(ExpectIntrinsicsHandled,
"Number of 'expect' intrinsic instructions handled");

// These default values are chosen to represent an extremely skewed outcome for
// a condition, but they leave some room for interpretation by later passes.
//
// If the documentation for __builtin_expect() was made explicit that it should
// only be used in extreme cases, we could make this ratio higher. As it stands,
// programmers may be using __builtin_expect() / llvm.expect to annotate that a
// branch is likely or unlikely to be taken.
//
// There is a known dependency on this ratio in CodeGenPrepare when transforming
// 'select' instructions. It may be worthwhile to hoist these values to some
// shared space, so they can be used directly by other passes.

cl::opt<uint32_t> llvm::LikelyBranchWeight(
"likely-branch-weight", cl::Hidden, cl::init(2000),
cl::desc("Weight of the branch likely to be taken (default = 2000)"));
cl::opt<uint32_t> llvm::UnlikelyBranchWeight(
"unlikely-branch-weight", cl::Hidden, cl::init(1),
cl::desc("Weight of the branch unlikely to be taken (default = 1)"));

static std::tuple<uint32_t, uint32_t>
getBranchWeight(Intrinsic::ID IntrinsicID, CallInst *CI, int BranchCount) {
if (IntrinsicID == Intrinsic::expect) {
Expand Down

0 comments on commit ee8b538

Please sign in to comment.