2

__builtin_expect from GCC can be used by programmer to show which variants are expected to be very often and which are rare. But __builtin_expect have only "true" and "false" (0% or 100% probability)

For some big projects it is vary hard to get profile feedback (-fprofile-arcs), and sometimes programmer does know, what probability of branch he have in some point of program.

It is possible to give a hint to compiler that a branch have probability >0% and <100% ?

osgx
  • 90,338
  • 53
  • 357
  • 513
  • If "true" and "false" really meant 100% or 0%, the compiler could just drop the impossible branch altogether. It makes more sense if they already mean 0

    – aschepler Nov 16 '10 at 14:32

3 Answers3

9

From here:

long __builtin_expect_with_probability

(long exp, long c, double probability) The function has the same semantics as __builtin_expect, but caller provides the expected probability that exp == c. Last argument, probability, is a floating-value in the inclusive range 0.0f and 1.0f. The probability argument must be constant floating-point expression.

Jesin pointed out in the comments, Clang 11 has it too.

MCCCS
  • 1,002
  • 3
  • 20
  • 44
3

True and false really mean that "the first variant is more likely" and "the second variant is more likely". There's no practical need for any values other than these. The compiler won't be able to use that information.

Eugene Smith
  • 9,126
  • 6
  • 36
  • 40
  • 1
    user434507, are you sure about "The compiler won't be able to use that information." ? – osgx Nov 17 '10 at 02:25
  • 4
    "The compiler won't be able to use that information". I strongly disagree. There are various places where a more fine-grained information is valuable in a compiler (branch prediction, register allocation, vectorization, ...). LLVM, for instance, can represent frequency information directly in the IR, although AFAIK this is not exposed to the user (http://llvm.org/docs/BlockFrequencyTerminology.html). – madmann91 Jun 28 '17 at 21:46
-4

Non-determinism is not a desirable trait for compiler output, let alone language features. There is no real benefit to only partial optimization preferring one branch, and no compiler I'm aware of can do this.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526