Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/include/llvm/Analysis/BranchProbabilityInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
#define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/DenseSet.h"
Expand Down Expand Up @@ -181,7 +182,7 @@ class BranchProbabilityInfo {
/// can be used when updating the CFG to update the branch probability
/// information.
void setEdgeProbability(const BasicBlock *Src,
const SmallVectorImpl<BranchProbability> &Probs);
ArrayRef<BranchProbability> Probs);

/// Copy outgoing edge probabilities from \p Src to \p Dst.
///
Expand Down Expand Up @@ -406,6 +407,7 @@ class BranchProbabilityInfo {
/// Based on computed weights by \p computeEstimatedBlockWeight set
/// probabilities on branches.
bool calcEstimatedHeuristics(const BasicBlock *BB);
bool calcFixedWeights(const BasicBlock *BB);
bool calcMetadataWeights(const BasicBlock *BB);
bool calcPointerHeuristics(const BasicBlock *BB);
bool calcZeroHeuristics(const BasicBlock *BB, const TargetLibraryInfo *TLI);
Expand Down
18 changes: 17 additions & 1 deletion llvm/lib/Analysis/BranchProbabilityInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,20 @@ void BranchProbabilityInfo::computeEestimateBlockWeight(
} while (!BlockWorkList.empty() || !LoopWorkList.empty());
}

bool BranchProbabilityInfo::calcFixedWeights(const BasicBlock *BB) {
const Instruction *Terminator = BB->getTerminator();
if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(Terminator)) {
assert(Invoke->getNormalDest() == Invoke->getSuccessor(0) &&
Invoke->getUnwindDest() == Invoke->getSuccessor(1) &&
"unexpected successor ordering");
const BranchProbability BP[] = {BranchProbability::getOne(),
BranchProbability::getZero()};
setEdgeProbability(BB, BP);
return true;
}
return false;
}

// Calculate edge probabilities based on block's estimated weight.
// Note that gathered weights were not scaled for loops. Thus edges entering
// and exiting loops requires special processing.
Expand Down Expand Up @@ -1130,7 +1144,7 @@ BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,

/// Set the edge probability for all edges at once.
void BranchProbabilityInfo::setEdgeProbability(
const BasicBlock *Src, const SmallVectorImpl<BranchProbability> &Probs) {
const BasicBlock *Src, ArrayRef<BranchProbability> Probs) {
assert(Src->getTerminator()->getNumSuccessors() == Probs.size());
eraseBlock(Src); // Erase stale data if any.
if (Probs.size() == 0)
Expand Down Expand Up @@ -1256,6 +1270,8 @@ void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LoopI,
continue;
if (calcMetadataWeights(BB))
continue;
if (calcFixedWeights(BB))
continue;
if (calcEstimatedHeuristics(BB))
continue;
if (calcPointerHeuristics(BB))
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/Analysis/BranchProbabilityInfo/basic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ entry:
if.then:
invoke i32 @InvokeCall()
to label %invoke.cont unwind label %lpad
; CHECK: edge if.then -> invoke.cont probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge if.then -> lpad probability is 0x00008000 / 0x80000000 = 0.00%
; CHECK: edge if.then -> invoke.cont probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge if.then -> lpad probability is 0x00000000 / 0x80000000 = 0.00%

invoke.cont:
call void @ColdFunc() #0
Expand All @@ -270,8 +270,8 @@ if.then:
invoke i32 @InvokeCall()
to label %invoke.cont unwind label %lpad
; The cold call heuristic should not kick in when the cold callsite is in EH path.
; CHECK: edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
; CHECK: edge if.then -> invoke.cont probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge if.then -> lpad probability is 0x00000000 / 0x80000000 = 0.00%

invoke.cont:
br label %if.end
Expand All @@ -298,8 +298,8 @@ if.then:
to label %invoke.cont unwind label %lpad
; Regardless of cold calls, edge weights from a invoke instruction should be
; determined by the invoke heuristic.
; CHECK: edge if.then -> invoke.cont probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge if.then -> lpad probability is 0x00008000 / 0x80000000 = 0.00%
; CHECK: edge if.then -> invoke.cont probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge if.then -> lpad probability is 0x00000000 / 0x80000000 = 0.00%

invoke.cont:
call void @ColdFunc() #0
Expand All @@ -318,13 +318,13 @@ if.end:
; CHECK-LABEL: test_invoke_code_profiled
define void @test_invoke_code_profiled(i1 %c) personality ptr @__gxx_personality_v0 {
entry:
; CHECK: edge entry -> invoke.to0 probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge entry -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
; CHECK: edge entry -> invoke.to0 probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge entry -> lpad probability is 0x00000000 / 0x80000000 = 0.00%
invoke i32 @InvokeCall() to label %invoke.to0 unwind label %lpad

invoke.to0:
; CHECK: edge invoke.to0 -> invoke.to1 probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge invoke.to0 -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
; CHECK: edge invoke.to0 -> invoke.to1 probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge invoke.to0 -> lpad probability is 0x00000000 / 0x80000000 = 0.00%
invoke i32 @InvokeCall() to label %invoke.to1 unwind label %lpad,
!prof !{!"branch_weights", i32 444}

Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Analysis/BranchProbabilityInfo/deopt-invoke.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ declare void @cold() cold
define void @test1(i32 %0) personality ptr @"personality_function" !prof !1 {
;CHECK: edge entry -> unreached probability is 0x00000001 / 0x80000000 = 0.00%
;CHECK: edge entry -> invoke probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
;CHECK: edge invoke -> invoke.cont.unreached probability is 0x00000000 / 0x80000000 = 0.00%
;CHECK: edge invoke -> land.pad probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
;CHECK: edge invoke -> invoke.cont.unreached probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
;CHECK: edge invoke -> land.pad probability is 0x00000000 / 0x80000000 = 0.00%
;CHECK: edge land.pad -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]

entry:
Expand Down Expand Up @@ -41,8 +41,8 @@ exit:
define void @test2(i32 %0) personality ptr @"personality_function" {
;CHECK: edge entry -> unreached probability is 0x00000000 / 0x80000000 = 0.00%
;CHECK: edge entry -> invoke probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
;CHECK: edge invoke -> invoke.cont.cold probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
;CHECK: edge invoke -> land.pad probability is 0x00008000 / 0x80000000 = 0.00%
;CHECK: edge invoke -> invoke.cont.cold probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
;CHECK: edge invoke -> land.pad probability is 0x00000000 / 0x80000000 = 0.00%
;CHECK: edge land.pad -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]

entry:
Expand Down Expand Up @@ -71,8 +71,8 @@ exit:
define void @test3(i32 %0) personality ptr @"personality_function" {
;CHECK: edge entry -> unreached probability is 0x00000000 / 0x80000000 = 0.00%
;CHECK: edge entry -> invoke probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
;CHECK: edge invoke -> invoke.cont.cold probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
;CHECK: edge invoke -> land.pad probability is 0x00008000 / 0x80000000 = 0.00%
;CHECK: edge invoke -> invoke.cont.cold probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
;CHECK: edge invoke -> land.pad probability is 0x00000000 / 0x80000000 = 0.00%
;CHECK: edge land.pad -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
entry:
br i1 undef, label %unreached, label %invoke
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Analysis/BranchProbabilityInfo/loop.ll
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ loop:
%i.0 = phi i32 [ 0, %entry ], [ %inc, %invoke.cont ]
invoke i32 @InvokeCall()
to label %invoke.cont unwind label %lpad
; CHECK: edge loop -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge loop -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
; CHECK: edge loop -> invoke.cont probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
; CHECK: edge loop -> lpad probability is 0x00000000 / 0x80000000 = 0.00%

invoke.cont:
%inc = add nsw i32 %i.0, 1
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Analysis/BranchProbabilityInfo/noreturn.ll
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ if.then: ; preds = %entry
%exception = call ptr @__cxa_allocate_exception(i64 1) #0
invoke i32 @smallFunction(i32 %idx)
to label %invoke.cont unwind label %lpad
; CHECK: edge if.then -> invoke.cont probability is 0x40000000 / 0x80000000 = 50.00%
; CHECK: edge if.then -> lpad probability is 0x40000000 / 0x80000000 = 50.00%
; CHECK: edge if.then -> invoke.cont probability is 0x80000000 / 0x80000000 = 100.00%
; CHECK: edge if.then -> lpad probability is 0x00000000 / 0x80000000 = 0.00%

invoke.cont: ; preds = %if.then
call void @__cxa_throw(ptr %exception, ptr @_ZTIi, ptr null) #1
Expand Down