How can I tell llvm, that a branch is preferred ?

If I read the llvm language correctly, it doesn't have a way to specify the preferred branch, correct ? I see nothing in the specs for "branch" or "switch". And __buildin_expect does nothing, that I am sure of.

Unfortunately llvm has this knack for ordering my one most crucial part of code exactly the opposite I want to, it does: (x86_64)

  cmpq %r15, (%rax,%rdx)
  jne LBB0_3
Ltmp18:
  leaq 8(%rax,%rdx), %rcx
  jmp LBB0_4
LBB0_3:
  addq $8, %rcx
LBB0_4:

when I want,

  cmpq %r15, (%rax,%rdx)
  jeq LBB0_3

  addq $8, %rcx
  jmp LBB0_4
LBB0_3:
  leaq 8(%rax,%rdx), %rcx
LBB0_4:

since that saves me executing a jump 99.9% of the time. Is there anything I can do ?

Ciao
    Nat!

Hi Nat!,

I think you can try to attach branch weight metadata to the branch or switch, and give a higher weight to the likely taken path. In that way, LLVM should be able to order the code correctly.

For branch weight metadata, you can refer to the following document:

http://llvm.org/docs/BranchWeightMetadata.html

thanks,
chen

I haven't tried myself, but the documentation claims that
__builtin_expect should work:
http://llvm.org/docs/BranchWeightMetadata.html#builtin-expect

Can you turn your code into an example we can try?

- Hans

Well, you could generate a !prof annotation to specify branch weights that
are then used when computing branch probability and block frequencies (
http://llvm.org/docs/BranchWeightMetadata.html)

Diego.

Thanks, sounds good. I will try out.

Ciao
   Nat!

This somehow reminds me on this old anecdote: