Real Time Systems Design And Analysis

Chapter 7.6.3. - Common Subexpression Elimination

7.6.3   Common Subexpression Elimination
Repeated calculations of the same subexpression in two different equations should
be avoided. For example, the following C program fragment:

x=6+a*b;
y=a*b+z;


could be replaced with

t=a*b;
x=y+t;
y=t+z;


thus eliminating the additional multiplication. This can result in significant savings
if a and b are floating-point numbers and the code occurs in a tight loop.


7.6.4   Intrinsic Functions

When possible, use intrinsic functions rather than ordinary functions. Intrinsic
functions are simply macros where the actual function call is replaced by inline
code during compilation. This improves real-time performance because the
need to pass parameters, create space for local variables, and release that space,
is eliminated.


7.6.5   Constant Folding

Most compilers perform constant folding, but this should not be assumed. As an
example, the statement:

x=2.0*x*4.0;

would be optimized by folding 2.0 * 4.0 into 8.0. Performing this operation
manually leads to code that is easier to debug, because the programmer performs
the optimization explicitly. And although the original statement may be more
descriptive, a comment can be provided to explain the optimized statement.

For example, if the program uses π/2 it should be precomputed during the
initialization and stored as a constant named, for example, pi_div_2. This
will typically save one floating-point load and one floating-point divide instruction
– potentially tens of microseconds. In a 5-millisecond real-time cycle, this
can lead to time-loading savings of 0.1%. Incidentally, using this strategy again
illustrates the inverse relationship between time and memory utilization: code
execution time has been reduced, but extra memory is needed to store the precomputed
constant.


7.6.6   Loop Invariant Optimization

Most compilers will move computations outside loops that do not need to be performed
with the loop, a process called loop invariant optimization. For example,
consider the following code fragment in C:

x=100;
while (x>0)
  x = x-y+z;


it can be replaced by

x=100;
t=y+z;

while(x>0)
  x=x-t;


This moves an instruction outside the loop, but requires additional memory.


7.6.7   Loop Induction Elimination

A variable i is called an induction variable of a loop if every time i changes
its value, and it is incremented or decremented by some constant. A common
situation is one in which the induction variable is i and another variable, j,
which is a linear function if it, is used to offset into some array. Often i is used
only for a test of loop termination. Variable i can be eliminated by replacing its
test for one on j. For example, consider the following C program fragment:

for (i=1,i<=10;i++)
  a [i+1] = 1;

an optimized version is

for (i=2,i<=11;i++)
  a[j] = 1;


eliminating the extra addition within the loop.

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: PCMCIA Memory Cards
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.