سؤال

In this question I will focus on Visual Studio 2012 and GCC 4.7

On the one hand, profile-guided compilation optimizes branch prediction by instrumenting the code at run-time, and then using this information during a second compilation. On the other hand, many compilers provide extensions to hint to the compiler which branch will most-likely be taken: GCC provides __builtin_expect and Visual Studio provides __assume.

Is there a way to extract the profile-guided information (of either compiler) so as to be able to rewrite the code using both compiler extensions? The goal would be to provide an "optimized" source code for people willing to recompile their own version of the binary.

هل كانت مفيدة؟

المحلول

You can extract this information using gcov.

Compile the sources like:

gcc -ftest-coverage -fprofile-arcs x.c

Run the executable

./a.out

Run gcov -b on a source file. The option -b tells it to generate branch probability information.

gcov -b x.c

gcov will create text file x.c.gcov, which will contain the required information, here's a sample:

        -:    5:int
function foo called 1 returned 100% blocks executed 100%
        1:    6:foo (unsigned int N)
        -:    7:{
        1:    8:  int i, s = 0;
        -:    9:
    10001:   10:  for (i = 0; i < N; ++i)
branch  0 taken 99%
branch  1 taken 1% (fallthrough)
        -:   11:    {
    10000:   12:      if ((rand () % 100) < 30)
call    0 returned 100%
branch  1 taken 30% (fallthrough)
branch  2 taken 70%
     3027:   13:        s++;
        -:   14:      else
     6973:   15:        s--;
        -:   16:    }
        -:   17:
        1:   18:  return s;
        -:   19:}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top