Skip to content

Commit ee8b538

Browse files
committed
[BranchProbability] move options for 'likely' and 'unlikely'
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
1 parent 47fdaa3 commit ee8b538

File tree

5 files changed

+20
-23
lines changed

5 files changed

+20
-23
lines changed

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
#include "llvm/IR/Intrinsics.h"
4343
#include "llvm/IR/MDBuilder.h"
4444
#include "llvm/IR/Operator.h"
45+
#include "llvm/Support/BranchProbability.h"
4546
#include "llvm/Support/CRC.h"
46-
#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
4747
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
4848
using namespace clang;
4949
using namespace CodeGen;

llvm/include/llvm/Support/BranchProbability.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
1414
#define LLVM_SUPPORT_BRANCHPROBABILITY_H
1515

16+
#include "llvm/Support/CommandLine.h"
1617
#include "llvm/Support/DataTypes.h"
1718
#include <algorithm>
1819
#include <cassert>
@@ -21,6 +22,9 @@
2122

2223
namespace llvm {
2324

25+
extern cl::opt<uint32_t> LikelyBranchWeight;
26+
extern cl::opt<uint32_t> UnlikelyBranchWeight;
27+
2428
class raw_ostream;
2529

2630
// This class represents Branch Probability as a non-negative fraction that is

llvm/include/llvm/Transforms/Scalar/LowerExpectIntrinsic.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include "llvm/IR/Function.h"
1919
#include "llvm/IR/PassManager.h"
20-
#include "llvm/Support/CommandLine.h"
2120

2221
namespace llvm {
2322

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

35-
extern cl::opt<uint32_t> LikelyBranchWeight;
36-
extern cl::opt<uint32_t> UnlikelyBranchWeight;
3734
}
3835

3936
#endif

llvm/lib/Support/BranchProbability.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919

2020
using namespace llvm;
2121

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

2438
raw_ostream &BranchProbability::print(raw_ostream &OS) const {

llvm/lib/Transforms/Scalar/LowerExpectIntrinsic.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/IR/Metadata.h"
2525
#include "llvm/InitializePasses.h"
2626
#include "llvm/Pass.h"
27+
#include "llvm/Support/BranchProbability.h"
2728
#include "llvm/Support/Debug.h"
2829
#include "llvm/Transforms/Scalar.h"
2930

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

37-
// These default values are chosen to represent an extremely skewed outcome for
38-
// a condition, but they leave some room for interpretation by later passes.
39-
//
40-
// If the documentation for __builtin_expect() was made explicit that it should
41-
// only be used in extreme cases, we could make this ratio higher. As it stands,
42-
// programmers may be using __builtin_expect() / llvm.expect to annotate that a
43-
// branch is likely or unlikely to be taken.
44-
//
45-
// There is a known dependency on this ratio in CodeGenPrepare when transforming
46-
// 'select' instructions. It may be worthwhile to hoist these values to some
47-
// shared space, so they can be used directly by other passes.
48-
49-
cl::opt<uint32_t> llvm::LikelyBranchWeight(
50-
"likely-branch-weight", cl::Hidden, cl::init(2000),
51-
cl::desc("Weight of the branch likely to be taken (default = 2000)"));
52-
cl::opt<uint32_t> llvm::UnlikelyBranchWeight(
53-
"unlikely-branch-weight", cl::Hidden, cl::init(1),
54-
cl::desc("Weight of the branch unlikely to be taken (default = 1)"));
55-
5638
static std::tuple<uint32_t, uint32_t>
5739
getBranchWeight(Intrinsic::ID IntrinsicID, CallInst *CI, int BranchCount) {
5840
if (IntrinsicID == Intrinsic::expect) {

0 commit comments

Comments
 (0)