Jun 2, 2022

[compiler][optimization] jump threading optimizations intro

Reference:
A gentle introduction to jump threading optimizations
https://developers.redhat.com/blog/2019/03/13/intro-jump-threading-optimizations

cmove:
https://stackoverflow.com/questions/30150274/purpose-of-cmove-instruction-in-x86-assembly


Code duplication is the key for the optimization trade-off.



gcc options:
  • --param max-fsm-paths-insns=500
  • --param max-fsm-thread-length
  • -fno-thread-jumps
  • -fdump-tree-all-details -O2

code in action

example 1
if (a > 5)
    goto j;
  stuff ();
  stuff ();
j:
  goto somewhere;
to
if (a > 5)
    goto somewhere;
  stuff ();
  stuff ();
j:
  goto somewhere;

example 2
void foo(int a, int b, int c)
{
  if (a && b)
    foo ();
  if (b || c)
    bar ();
}
to
void foo(int a, int b, int c)
{
  if (a && b) {
    foo ();
    goto skip;
  }
  if (b || c) {
skip:
    bar ();
  }
}

example 3
void foo(int a, int b, int c)
{
  if (a && b)
    foo ();
  tweak ();
  if (b || c)
  bar ();
}
to
void foo(int a, int b, int c)
{
  if (a && b) {
    foo ();
    tweak ();
    goto skip;
  }
  tweak ();
  if (b || c) {
skip:
    bar ();
  }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.