Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Mix Tutorials

Asterisk
Website Building

Novels

Mix Novels

Human Personality

Body Language
Code Optimization Using the GNU C Compiler Print E-mail
Article Index
Code Optimization Using the GNU C Compiler
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7

5. Dead Code Elimination

Dead code is the code in the program that will never be executed for any input or other conditions. A common example is an if statement. If the compiler finds out that the condition inside the if is never going to be true, then the body of the if statement will never be executed. In that case, the compiler can completely eliminate this dead code, thus saving the memory space occupied by the code. The following program demonstrates dead code elimination.


1 : /* test4.c */
2 : /* demonstration of dead code elimination */
3 : #include <stdio.h>
4 :
5 : int main()
6 : {
7 : int x;
8 :
9 : scanf("%d", &x);
10 :
11 : if(x < 0 && x > 0)
12 : {
13 : x = 99;
14 : printf("Hello. Inside the if!!!");
15 : }
16 :
17 : return 0;
18 : }
19 : /* end of test4.c */
20 :
21 : /* --------------------------------------------------------------- */
22 : /* optimized assembly code */
23 : .file "test4.c"
24 : .version "01.01"
25 : gcc2_compiled.:
26 : .section .rodata
27 : .LC0:
28 : .string "%d"
29 : .LC1:
30 : .string "Hello. Inside the if!!!"
31 : .text
32 : .align 4
33 : .globl main
34 : .type main,@function
35 : main:
36 : pushl %ebp /* save EBP on stack */
37 : movl %esp,%ebp /* EBP = ESP */
38 : subl $4,%esp /* create space for x on the stack */
39 :
40 : /* scanf("%d", &x); */
41 : leal -4(%ebp),%eax
42 : pushl %eax /* push address of a on stack */
43 : pushl $.LC0 /* push string on stack */
44 : call scanf
45 :
46 : /* the entire body of the if and the condition checking is dead code */
47 : /* return 0; */
48 : xorl %eax,%eax /* no stack cleanup, we are exiting anyway */
49 : leave
50 : ret
51 : .Lfe1:
52 : .size main,.Lfe1-main
53 : .ident "GCC: (GNU) egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)"
54 : /* end optimized assembly code */
55 : /* ---------------------------------------------------------------- */

Since the unoptimized assembly language code serves no purpose and also its easy to understand, I have omitted it. In the program, the condition on line 11 is x < 0 && x > 0, which can never be true. The compiler finds this and concludes that the body of the if statement forms dead code, so it does not generate any code for that. One interesting thing to notice is that after the body of if has been eliminated, the string "Hello. Inside the if!!!" is no longer needed and hence can be eliminated from the read-only data section. However, detection of such things probably will increase the complexity of the compiler and hence is not done.


 
< Prev   Next >
Your Ad Here

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits
BargainCouponCode.com: discounts coupons for 100s of online retail stores