Thread: Do compilers remove unused code?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    32

    Do compilers remove unused code?

    This is perhaps a dumb question, but do compilers remove included or written code if it's never used by rest of the program?

  2. #2
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    They shouldn't, and if yours does, you might need a new compiler, but let's check anyway:
    Code:
    #include <stdio.h>
    
    void data() {
    	printf("I really dont do anything at all\n");
    }
    int main() {
    	printf("Hello World!");
    	return 0;
    }
    C:\WINDOWS\Desktop>C:\mingw\bin\gcc -S test.c

    C:\WINDOWS\Desktop>
    (The -S tag is simply for outputting a .S (assembler) file instead of compiling straight to binary. You could also use -save-temps but -S is what we would rather have, it's not an optimization flag.)
    MinGW without -O2
    Code:
    ...
    LC0:
    	.ascii "I really dont do anything at all\12\0"
    	.text
    .globl _data
    	.def	_data;	.scl	2;	.type	32;	.endef
    _data:
    	pushl	%ebp
    	movl	%esp, %ebp
    	subl	$8, %esp
    	movl	$LC0, (%esp)
    	call	_printf
    	leave
    	ret
    ...
    MinGW with -O2:
    Code:
    LC0:
    	.ascii "I really dont do anything at all\0"
    	.text
    	.p2align 4,,15
    .globl _data
    	.def	_data;	.scl	2;	.type	32;	.endef
    _data:
    	pushl	%ebp
    	movl	%esp, %ebp
    	subl	$8, %esp
    	movl	$LC0, (%esp)
    	call	_puts
    	leave
    	ret
    Nope. Let's even go into passing the strip tag.
    MinGW with -O3 and -s:

    Code:
    LC0:
    	.ascii "I really dont do anything at all\0"
    	.text
    	.p2align 4,,15
    .globl _data
    	.def	_data;	.scl	2;	.type	32;	.endef
    _data:
    	pushl	%ebp
    	movl	%esp, %ebp
    	subl	$8, %esp
    	movl	$LC0, (%esp)
    	call	_puts
    	leave
    	ret
    Nope. Doesn't remove anything. Optimizations aside, your code will bascially come out the same.
    Last edited by Mad_guy; 12-26-2005 at 02:08 PM.
    operating systems: mac os 10.6, debian 5.0, windows 7
    editor: back to emacs because it's more awesomer!!
    version control: git

    website: http://0xff.ath.cx/~as/

  3. #3
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    This is perhaps a dumb question, but do compilers remove included or written code if it's never used by rest of the program?
    No, they shouldn't, as state above. But you might be confusing the compiler with the linker, which might not link unused object code in the final executable. For example, if you include stdio.h but never use printf, the object code for printf doesn't necessarily have to be linked in.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I know VC++ 6.0 will throw out unused code during optimization stage -- the compiler is documented to do that. So if you write a function foo() that is never called the compiler will discard it in the final exe program.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    It depends how obvious the omission is, and it depends on the compiler/linker, and I'm not sure what one gains from this knowledge. The C standard doesn't care.
    My compiler will include the printf call and the "Hello, world!\n" string literal if I do:
    Code:
    #include <stdio.h>
    int main(void)
    {
        int a = 5;
        if (a==5)
            return 0;
        printf("Hello, world!\n");
        return 0;
    }
    But not if I change a == 5 to a == a.

    However, if I keep it as a == a, but change a's type to a double, it includes the code again, since a double does not always return true when compared to itself.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    They will optimize un-optimized loops and they will remove redundant code if specified. This can change the final assembly to something you might not expect.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but let's check anyway:
    Repeat your experiment with a static function.

    Attempts like
    /* waste a bit of time */
    for ( i = 0 ; i < 1000000 ; i++ );
    Often cause surprise to newbies when they finally figure out that the compiler got rid of the loop because it didn't do anything useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM