HC08 Branch Optimizations

This optimization minimizes the span of branch instructions. The Compiler replaces a relative branch by an inverted condition branch across an unconditional jump, unless the offset of this branch is in the range [-128 to 127]:

    BRcc dest

  
    ...; More than 127 bytes of code

  
  dest:

  

Provided the code indicated by "..." doesn't contain another branch to label dest, the Compiler changes the previous code to:

    BR~cc skip

  
    JMP   dest

  
  skip:

  
    ...    ; More than 127 bytes of code

  
  dest:

  

Also, the Compiler may resolve branches to branches into two branches to the same target. The Compiler may remove redundant branches (for example, a branch to the instruction immediately following it).

The code above has the following effect:

  1. The opcode byte of BRN (branch never) replaces an unconditional branch over one byte.
  2. The next byte is skipped (opcode decoded as SKIP1).
  3. The opcode of CPHX < immediate_16> replaces an unconditional branch over two bytes, if no flags are needed afterwards.
  4. The following two bytes are skipped (opcode decoded as SKIP2).
  5. One byte is gained for SKIP1 and two bytes are gained for SKIP2.

The execution speed remains unchanged. Use the -OnB: Disable Branch Optimizer compiler option to disable this optimization.