19 Code Optimization 17-02-2025
19 Code Optimization 17-02-2025
Program Optimization
Methodologies for program optimisation
Expression simplification
Dead Code Elimination
Procedure Inlining
Loop transformation
Program Optimization – Expression Simplification
VIT University
Contd..
Since the new expression has
only two operations rather than
three for the original form, it is
almost certainly cheaper,
because it is both faster and
smaller
a*b + a*c=a*(b + c)
Program Optimization – Expression
Simplification
We can also use the laws
of arithmetic to further
simplify expressions on
constants
Consider the following C
statement:
for (i = 0; i < 8 + 1; i+
+)
VIT University
Contd..
We can simplify 8+ 1 to 9 at compile time
there is no need to perform that
arithmetic while the program is
executing
Why would a program ever contain
expressions that evaluate to constants?
Using named constants rather than
numbers is good programming practice
and often leads to constant expression
Program Optimization
Methodologies for program optimization
Expression simplification
Dead Code Elimination
Procedure Inlining
Loop transformation
Program Optimization – Dead Code
Elimination
#define DEBUG 0
...
if (DEBUG) print_debug_stuff();
In the above case, the print_debug_stuff( ) function is never
executed, but the code allows the programmer to override the
preprocessor variable definition (perhaps with a compile-time flag)
to enable the debugging code
VIT University
There are a few things to keep in mind
when using inline functions:
Inline functions should be small and simple.
The compiler is less likely to inline a large
or complex function.
Inline functions should be used sparingly.
Inlining too many functions can actually
make the program slower.
Inline functions should not be used for
recursive functions. The compiler cannot
inline a recursive function, and doing so
will likely result in a stack overflow.
Program Optimization
Methodologies for program optimization
Expression simplification
Dead Code Elimination
Procedure Inlining
Loop transformation
Loop Unrolling
Loop fusion
Loop fission
Loop Tiling
Program Optimization – Loop Transformations
VIT University
Contd..
Loop Overheads
A straightforward implementation of the
loop would
create and initialize the loop variable i ,
update its value on every iteration,
and test it to see whether to exit the loop
Rather than unroll the above loop four times, we could unroll it twice
The following code results:
for (i = 0; i < 2; i++) {
a[i*2] = b[i*2]*c[i*2];
a[i*2 + 1] = b[i*2 + 1]*c[i*2 + 1];
}
In this case, since all operations in the two lines of the loop body are
independent, later stages of the compiler may be able to generate
code that allows them to be executed efficiently on the CPU’s
pipeline
Program Optimization –Loop fusion
VIT University
Loop fusion Example
The two adjacent loops on the code fragment below can be fused into on
loop.
Below is the code
for (i = 0; i < 300;fragment
i++) after loop
a[i] = a[i] + 3; fusion.
for (i = 0; i < 300; i+
for (i = 0; i < 300;+)i++)
b[i] = b[i] + 4; {
a[i] = a[i] + 3;
b[i] = b[i] + 4;
Loop fusion Caution to use
Notes:
Loop fusion is not commonly supported by C compilers.
Although loop fusion reduces loop overhead, it does not
always improve run-time performance, and may reduce
run-time performance. For example, the memory
architecture may provide better performance if two
arrays are initialized in separate loops, rather than
initializing both arrays simultaneously in one loop.
Loop fission
Loop distribution (Loop fission) is
the opposite of loop fusion, that
is, decomposing a single loop into
multiple loops
int i, a[100],
int i, a[100],
b[100]; b[100];
for (i = 0; i < for (i = 0; i < 100;
100; i++) { i++) {
a[i] = 1; a[i] = 1;
b[i] = 2; }
}
for (i = 0; i < 100;
is equivalent
Loop tiling
Basic Idea:
1.