10

Have I understood this right, if statements are more dependent on branch prediction and v-table look-up is more dependent on branch target prediction? Regarding v-tables, there is no "branch prediction", just the target prediction?

Trying to understand how a v-table is processed by the CPU.

rohit89
  • 5,745
  • 2
  • 25
  • 42
user997112
  • 29,025
  • 43
  • 182
  • 361
  • 1
    Lets close a question because its only 4 lines long..... – user997112 Feb 06 '14 at 16:49
  • That's quite a snideful comment, I understand that an uncommented motion to close might seem unfair, but the reason is (somewhat) documented in the option selected (too broad) and you were not downvoted, which suggests that the voter thought this question could not be addressed efficiently on StackOverflow, which has more to do with StackOverflow itself and less with your question. AFAIK you are correct in your deduction: `if` and loops in general use *branch prediction* (boolean output) whilst function pointers/virtual functions use *branch target prediction* (target output). – Matthieu M. Feb 06 '14 at 16:58
  • How do I see the reason for closing? It cant be a broad question- there's two questions, both related and specific? (but I thank you for taking the time to answer my question via your comment) – user997112 Feb 06 '14 at 17:49
  • Ah! You might not have enough reputation to have access to the `close` dialogue :x – Matthieu M. Feb 06 '14 at 18:42

1 Answers1

9

Branch prediction is predicting whether or not the branch will be taken. Branch target prediction is prediction where the branch is going to. These two things are independent and can occur in all combinations.

Examples of these might be:

Unconditional branch, fixed target

  • Infinite loop
  • goto statement
  • break or continue statement
  • End of the 'then' clause of an if/else statement (to jump past the else clause)
  • Non-virtual function call

Unconditional branch, variable target

  • Returning from a function
  • Virtual function call
  • Function pointer call
  • switch statement (if compiled into a jump table)

Conditional branch, fixed target

  • if statement
  • switch statement (if compiled into a series of if/else statements)
  • Loop condition tests
  • The && and || operators
  • The ternary ?: operator

Conditional branch, variable target

  • Less likely to show up under normal conditions, but the compiler may synthesize one as an optimization, combining two of the above cases. For example, on x86, the compiler may optimize code like if (condition) { obj->VirtualFunctionCall(); } into a conditional indirect jump like jne *%eax if it appears at the end of a function due to tail call optimization.
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • Just looking at this: http://research.engineering.wustl.edu/~songtian/pdf/intel-haswell.pdf if you go to slide 13 where is the branch target predictor? – user997112 Feb 06 '14 at 18:01
  • 1
    @user997112 It's probably part of the upper left block called "Branch predictors", otherwise how would you feed the target into the fetch unit? – Leeor Feb 06 '14 at 20:18