Skip to content

Commit 664d0c0

Browse files
committed
[TargetTransformInfo] move branch probability query from TargetLoweringInfo
This is no-functional-change intended (NFC), but needed to allow optimizer passes to use the API. See D98898 for a proposed usage by SimplifyCFG. I'm simplifying the code by removing the cl::opt. That was added back with the original commit in D19488, but I don't see any evidence in regression tests that it was used. Target-specific overrides can use the usual patterns to adjust as necessary. We could also restore that cl::opt, but it was not clear to me exactly how to do it in the convoluted TTI class structure.
1 parent fe5f66d commit 664d0c0

File tree

7 files changed

+25
-22
lines changed

7 files changed

+25
-22
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/IR/PassManager.h"
2828
#include "llvm/Pass.h"
2929
#include "llvm/Support/AtomicOrdering.h"
30+
#include "llvm/Support/BranchProbability.h"
3031
#include "llvm/Support/DataTypes.h"
3132
#include "llvm/Support/InstructionCost.h"
3233
#include <functional>
@@ -328,6 +329,10 @@ class TargetTransformInfo {
328329
return getUserCost(U, Operands, CostKind);
329330
}
330331

332+
/// If a branch or a select condition is skewed in one direction by more than
333+
/// this factor, it is very likely to be predicted correctly.
334+
BranchProbability getPredictableBranchThreshold() const;
335+
331336
/// Return true if branch divergence exists.
332337
///
333338
/// Branch divergence has a significantly negative impact on GPU performance
@@ -1400,6 +1405,7 @@ class TargetTransformInfo::Concept {
14001405
BlockFrequencyInfo *BFI) = 0;
14011406
virtual int getUserCost(const User *U, ArrayRef<const Value *> Operands,
14021407
TargetCostKind CostKind) = 0;
1408+
virtual BranchProbability getPredictableBranchThreshold() = 0;
14031409
virtual bool hasBranchDivergence() = 0;
14041410
virtual bool useGPUDivergenceAnalysis() = 0;
14051411
virtual bool isSourceOfDivergence(const Value *V) = 0;
@@ -1691,6 +1697,9 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
16911697
TargetCostKind CostKind) override {
16921698
return Impl.getUserCost(U, Operands, CostKind);
16931699
}
1700+
BranchProbability getPredictableBranchThreshold() override {
1701+
return Impl.getPredictableBranchThreshold();
1702+
}
16941703
bool hasBranchDivergence() override { return Impl.hasBranchDivergence(); }
16951704
bool useGPUDivergenceAnalysis() override {
16961705
return Impl.useGPUDivergenceAnalysis();

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

+10
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ class TargetTransformInfoImplBase {
7878
return TTI::TCC_Expensive;
7979
}
8080

81+
// Although this default value is arbitrary, it is not random. It is assumed
82+
// that a condition that evaluates the same way by a higher percentage than
83+
// this is best represented as control flow. Therefore, the default value N
84+
// should be set such that the win from N% correct executions is greater than
85+
// the loss from (100 - N)% mispredicted executions for the majority of
86+
// intended targets.
87+
BranchProbability getPredictableBranchThreshold() const {
88+
return BranchProbability(99, 100);
89+
}
90+
8191
bool hasBranchDivergence() const { return false; }
8292

8393
bool useGPUDivergenceAnalysis() const { return false; }

llvm/include/llvm/CodeGen/TargetLowering.h

-4
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,6 @@ class TargetLoweringBase {
528528
return false;
529529
}
530530

531-
/// If a branch or a select condition is skewed in one direction by more than
532-
/// this factor, it is very likely to be predicted correctly.
533-
virtual BranchProbability getPredictableBranchThreshold() const;
534-
535531
/// Return true if the following transform is beneficial:
536532
/// fold (conv (load x)) -> (load (conv*)x)
537533
/// On architectures that don't natively support some vector loads

llvm/lib/Analysis/TargetTransformInfo.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ int TargetTransformInfo::getUserCost(const User *U,
228228
return Cost;
229229
}
230230

231+
BranchProbability TargetTransformInfo::getPredictableBranchThreshold() const {
232+
return TTIImpl->getPredictableBranchThreshold();
233+
}
234+
231235
bool TargetTransformInfo::hasBranchDivergence() const {
232236
return TTIImpl->hasBranchDivergence();
233237
}

llvm/lib/CodeGen/CodeGenPrepare.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6587,7 +6587,7 @@ static bool isFormingBranchFromSelectProfitable(const TargetTransformInfo *TTI,
65876587
uint64_t Sum = TrueWeight + FalseWeight;
65886588
if (Sum != 0) {
65896589
auto Probability = BranchProbability::getBranchProbability(Max, Sum);
6590-
if (Probability > TLI->getPredictableBranchThreshold())
6590+
if (Probability > TTI->getPredictableBranchThreshold())
65916591
return true;
65926592
}
65936593
}

llvm/lib/CodeGen/TargetLoweringBase.cpp

-16
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#include "llvm/IR/IRBuilder.h"
4646
#include "llvm/IR/Module.h"
4747
#include "llvm/IR/Type.h"
48-
#include "llvm/Support/BranchProbability.h"
4948
#include "llvm/Support/Casting.h"
5049
#include "llvm/Support/CommandLine.h"
5150
#include "llvm/Support/Compiler.h"
@@ -114,17 +113,6 @@ static bool darwinHasSinCos(const Triple &TT) {
114113
return true;
115114
}
116115

117-
// Although this default value is arbitrary, it is not random. It is assumed
118-
// that a condition that evaluates the same way by a higher percentage than this
119-
// is best represented as control flow. Therefore, the default value N should be
120-
// set such that the win from N% correct executions is greater than the loss
121-
// from (100 - N)% mispredicted executions for the majority of intended targets.
122-
static cl::opt<int> MinPercentageForPredictableBranch(
123-
"min-predictable-branch", cl::init(99),
124-
cl::desc("Minimum percentage (0-100) that a condition must be either true "
125-
"or false to assume that the condition is predictable"),
126-
cl::Hidden);
127-
128116
void TargetLoweringBase::InitLibcalls(const Triple &TT) {
129117
#define HANDLE_LIBCALL(code, name) \
130118
setLibcallName(RTLIB::code, name);
@@ -1736,10 +1724,6 @@ bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
17361724
MMO.getAlign(), MMO.getFlags(), Fast);
17371725
}
17381726

1739-
BranchProbability TargetLoweringBase::getPredictableBranchThreshold() const {
1740-
return BranchProbability(MinPercentageForPredictableBranch, 100);
1741-
}
1742-
17431727
//===----------------------------------------------------------------------===//
17441728
// TargetTransformInfo Helpers
17451729
//===----------------------------------------------------------------------===//

llvm/lib/Transforms/Scalar/LowerExpectIntrinsic.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ STATISTIC(ExpectIntrinsicsHandled,
4646
// WARNING: these values are internal implementation detail of the pass.
4747
// They should not be exposed to the outside of the pass, front-end codegen
4848
// should emit @llvm.expect intrinsics instead of using these weights directly.
49-
// Transforms should use TargetLowering getPredictableBranchThreshold() hook.
49+
// Transforms should use TargetTransformInfo's getPredictableBranchThreshold().
5050
static cl::opt<uint32_t> LikelyBranchWeight(
5151
"likely-branch-weight", cl::Hidden, cl::init(2000),
5252
cl::desc("Weight of the branch likely to be taken (default = 2000)"));

0 commit comments

Comments
 (0)