User:Realazthat/Projects wishlist/LLVM/Tactical optimizations

From Rosetta Code

These are constant time optimizations; they only increase the program's execution speed by a constant. Almost all optimizations fit into this category.

Branching

<lang llvm>

  1. include <stdio.h>

int main() {

 int a, b;
 scanf("%d %d", &a, &b);

 if (a < b)
   printf("%d is less than %d\n", a, b);

 if (a == b)
   printf("%d is equal to %d\n", a, b);

 if (a > b)
   printf("%d is greater than %d\n", a, b);

 return 0;

} </lang> In the above code, LLVM actually does the third check. AFAICT, the third check is unnecessary; its analogous to an if-else-if-else statement.

Reloading registers from the stack memory

<lang llvm>

  1. include <stdio.h>

int main() {

 int a, b;
 scanf("%d %d", &a, &b);

 if (a < b)
   printf("%d is less than %d\n", a, b);

 if (a == b)
   printf("%d is equal to %d\n", a, b);

 if (a > b)
   printf("%d is greater than %d\n", a, b);

 return 0;

} </lang> In the above code, LLVM will allocate the two integers a, b, and after each function call reload then from the stack into registers for comparison, even after the printf calls, where the integers are passed by value.

Recursive to iterative functions

Automatically convert recursive functions to iterative with a stack

Eliminate the stack

Eliminate the stack where only the top is used (example of dynamic programming)

Reorder conditional branches

Reorder if-else statements/switches to have the most likely condition first to avoid checking all of the conditions.

  • Determine the likelihood of a condition
  • Determine how often a condition will be true if its deterministic