User:Realazthat/Projects wishlist/LLVM/Tactical optimizations

Revision as of 01:27, 18 October 2010 by rosettacode>Realazthat (Created page with "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 llv...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.