Optimizing a branch like a jump table?

62 views Asked by At

I was wondering if I have a branch

bool condition = x > y; // just an example
if(condition)
{
  // do the thing...
}
else
{
  // do the other thing...
}

It can be optimized to something like this because condition will be either 0 or 1, and rest of the code seems doable.

pseudo code:

1: condition = x > y    // just an example
2: jmp (!!condition)+3  // will either jump to line 3 or 4
3: jmp 5
4: jmp 20 // or any value known at compile time
5: // do the thing
.
.
20: // do the other thing
  1. Are branches optimized like this in common compilers?
  2. in what circumstances this is not possible? (if it's always possible, then why do we need branch prediction?)

Maybe my assumption is wrong and (!!condition) or x > y is already costing a branch?

0

There are 0 answers