Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1190

Appendix C: Notes on C


cantypeinanyvaluetheywant.Thereasonforusing this unwieldy method instead of just assigning a
specific value to thexvariable is simple. If the value ofxis fixed, a further optimization feature can be
applied (called dead code elimination, as described in the next section). This would change the code so
that the optimization feature discussed here would no longer be needed.

When a value is assigned toz, the program distinguishes two cases that depend on the size of the value
stored inx. What is common to both cases is that the expressionx*yis used in the assignment and, as
can be confirmed easily by humans, but with extreme difficulty by compilers, the variables used in both
branches of program flow do not change. The value previously computed as the assignment value forp
can therefore be reused. Again, the difficulty with this optimization feature lies not in the actual technical
replacement but in finding expressions that remain unchanged inall possible execution variants.

Dead Code Elimination


On first reading, the term ‘‘dead code elimination‘‘ sounds quite violent. On second reading, it seems
to be somewhat contradictory. After all, how cancode that is already dead be eliminated? Only when
the term is examined for a third time does it become apparent that it refers to an optimization feature in
which program sections that can never execute are eliminated from code generation to reduce the size of
the assembler code.

How does dead code accumulate in a program? It would be normal to expect programmers to give some
thought as to how their programs should run. And why should they waste their time writing superfluous
program fragments? This is indeed true forsimpleprograms, but the situation may well be different for
larger chunks of code that define a range of constants for specific program purposes. Elimination of
dead code is one of several important aspects when compiling C code for the architecture-independent
memory model discussed in detail in Chapter 3 (this model provides a uniform interface to the various
processors supported by the kernel). To understand how this optimization works take a look at the
following short example:

int x;
x=23;

if (x < 10) {
printf("x is less than 10!\n");
}
else {
printf("x is greater than or equal to 10!\n");
}

Without optimization, the following assembler code would be generated:

.file "dead.c"
.section .rodata
.LC0:
.string "x is less than 10!\n"
.align 32
.LC1:
.string "x is greater than or equal to 10!\n"
.text
.globl main
.type main,@function
main:
pushl %ebp
Free download pdf