title: Is it possible to mitigate branch misprediction penalty by giving earlier hint?
body: <p>TLDR: I want to give <em>runtime</em> branch prediction hits for x86-64, ideally if compiled by MSVC without asm, for a branch that is based on random data, by peeking into that data. Is it possible?</p>
  
  <hr>
  
  <p>Assume sequentially interpreting a byte stream, where variable-sized <code>StructureA</code> and <code>StructureB</code> occur, distinguished by some bit patterns, and they occur randomly with approximately equal probability.</p>
  
  <p>They have different functions to interpret them, so there's a branch dependent on data.</p>
  
  <p>As CPU will not be able to predict a random pattern, mispredictions are expected to delay execution.</p>
  
  <p>I see at least two ways I could provide information for branching in advance:</p>
  
  <ol>
  <li>By peeking forward into the stream</li>
  <li>By capturing bits, then by starting processing independently both <code>StructureA</code> and <code>StructureB</code>, interleaving code, hoping that out of order superscalar execution make the lines of code where I process <code>StructureA</code> and <code>StructureB</code> executing simultaneously, then branching, and discarding results for the wrong structure.</li>
  </ol>
  
  <p>I know there are (exotic) architectures which always employ delayed branching instead of branch prediction, so at least the second option would have worked. </p>
  
  <p>But is there a way to give such branch prediction hint on usual x86-64 ?</p>
  
  <p>I'm looking for machine instruction or something like this.</p>
  
  <p>Although if such thing exist, I ideally want it to compile as C++ code. 
  MSVC, x64, Windows 10, if these details matter.</p>
  
  <hr>
  
  <p>As it is apparently not possible, do following workarounds make sense:</p>
  
  <ol>
  <li>Always alternate branches, when there are consecutive <code>StructureA</code>, still process fake <code>StructureB</code>, so that the pattern is predictable.</li>
  <li>Always take both branches, discard results of wrong branch by <code>cmov</code></li>
  <li>Make some static hint with some sort of <code>[[likely]]</code> towards longer branch, assuming they are not equally fast.</li>
  </ol>
  
  <p>By make sense I mean whether they can they benefit, i.e. produce better throughput than control flow with unpredictable branches?</p>