Graphics Reference
In-Depth Information
13.2 Instruction Cache Optimizations
Optimizing the utilization of a cache has two key parts. The first is reducing the size
of the data it holds. The second is increasing data locality so that data accessed at the
same time is stored together. When writing assembly code, the programmer has direct
control over both parameters. However, when using high-level languages such as C
and C++ the programmer is somewhat at the mercy of the compiler to produce tight
machine code. Still, a number of things can be done to help the compiler produce more
code-cache-efficient code. For example, the following suggestions can help reduce
code size.
Be careful with inlining of code. Inlining tends to bloat code. Try to inline only
those functions that are small enough that the inlined code would be as small
as the code generated to call the function. Only functions meant to be inlined
should be defined in . h files. In addition, beware aliasing issues, as discussed in
Section 13.6.
Break loops up into cache-size chunks. When the body of a loop is too large to fit
in the instruction cache, break the code into several smaller loops. For example,
for(i=0;i<N;i++) {
...A...;
...B...;
...C...;
}
where A , B , and C correspond to large chunks of (independent) code, can instead
be written as
for(i=0;i<N;i++)
...A...;
for(i=0;i<N;i++)
...B...;
for(i=0;i<N;i++)
...C...;
where A , B , and C each fit in cache. The latter code allows A , B , and C to remain in
cache during the execution of the three smaller loops, rather than forcing each
other out of cache, as in the former code. Keeping the code in cache outweighs
the small cost added by the additional loop overhead. For loop bodies that do
not quite fit into the instruction cache, an opposite strategy can be employed.
Now try to reduce the code size to make the code fit in cache. Merge similar
loops using loop fusion; explicitly apply common-subexpression elimination to
repeated terms, rather than relying on the compiler being able to do so; and
 
Search WWH ::




Custom Search