By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,903 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

Dead Code elimination

P: 1

I have a question on dead code elimination.

Here is the test case:

Expand|Select|Wrap|Line Numbers
  1. #define TRUE 1
  2. #include <stdio.h>
  3. int no_def_fn(void);
  4. int main()
  5. {
  6.         if(TRUE)
  7.         {
  8.                 printf("Always TRUE");
  9.         }
  10.         else
  11.                 no_def_fn();
  12. }
$ gcc -g -c test_dead_code.c
$ nm -Ae test_dead_code.o | grep no
(returns nothing)
gcc version 3.3.3

Whereas in Sparc
CC -c -g test_dead_code.c
nm -Ae test_dead_code.o | grep no
test_dead_code.o: [6] | 0| 0|FUNC |GLOB |0 |UNDEF |__1cJno_def_fn6F_i_

Version of CC: Sun C++ 5.8 Patch 121017-08

In my understanding dead code elimination is done only during optimization. But why is gcc eliminating it in debug mode too?? When does this happen? Can someone give me some pointers on this?

Both gcc and sparc behave same way in optimized builds!

May 31 '07 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
Modern compilers are smarter than the old ones. Most of them when they see and infinite loop, will not compile anything in the function that follows that loop.

Also, if a return is placed in the middle of the function, th rest of the function may be dropped. You can check this in the debugger by placing a breakpoint beyond the return. You will neverbe able to get t the breakpoint because there is no code in the executable.

All of this has nothing to do wioth optimization.
May 31 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.