cleong
cleong

Reputation: 7606

Is redundant assignment elimination fairly typical optimization?

Given the following:

len = strlen(str);
/* code that does not read from len */
len = new_len;

Can I depend on the compiler to remove the first line?

I'm writing a code generating script. The first line is generated in one place, while everything that follows is generated elsewhere (by a virtual function in a descendant class). Most of the time, len isn't needed. Sometimes though, it is. I wonder if I can just set it in all cases and let the compiler get rid of the line if it isn't required.

Upvotes: 0

Views: 673

Answers (4)

cleong
cleong

Reputation: 7606

Modern compilers do seem to do an excellent job of eliminating redundant assignments. I ran the following snippet through VS 2008 and gcc 4.6 and the call to strlen (the inlined code, rather) is in fact removed. Both compilers managed to see that the both something() and something_else() produce no side effects. Calls to these are removed as well.

This occurs at /O1 in VC and /O1 in gcc.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int something(int len)  {
    if(len != len) {
        printf("%d\n", len);
    }
    return 0;
}

int something_else(int len) {
    return len * len;
}

int main(void) {
    char *s = malloc(1000);
    size_t len;
    strcpy(s, "Hello world");
    len = strlen(s);
    something(len);
    printf("%s\n", s);
    len += 8;
    something_else(len);
    len = 5;
    printf("%d\n", len);
    return 0;
}

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129374

Compilers are pretty clever. But to rely on the compiler removing "unused" function calls is probably not a good idea. For one thing, the compiler will NEED to understand the function you are calling (so strlen is a good example here, because most compilers understand what strlen does and how it affects other things) - if the function is not one that the compiler "understands", then it can't optimise it out.

What if you did:

 x = printf("Hello, World!\n");

 x = printf("World, Hello!\n");

Would you think they compiler had done the right thing by removing the first printf? Probably not... So, with any function the compiler can't determine "is side-effect free", the compiler MUST call the function, even if the result is not used. Side-effect free means under normal circumstances - e.g. there is a side-effect of calling strlen() with an invalid pointer - your code will probably crash - but that's not "normal circumstances" - you'd be pretty daft to use strlen() just to check if your pointer is a valid one or not, right?

So, in other words, you probably want to make sure your call to strlen() is really needed before you call strlen - or live with the fact that the compiler may wall generate an unnecessary strlen call.

Upvotes: 2

anishsane
anishsane

Reputation: 20980

It depends on compiler & the optimization level you specify.

Here it gets assigned initially from a function call. What if that function has a side effect?
So you cannot just assume the compiler to optimize it for you.

If the initial assignment statement is free from any side effects & you specify optimization level good enough (e.g. -O3 in case of gcc) it may optimize it for you.

Upvotes: 0

unwind
unwind

Reputation: 399833

No, of course you cannot "depend" on the optimization choices done by the compiler.

They can change at the user's whim (compiler command line options), or with different compiler versions.

Since the behavior you're describing is not required by the language standard, you cannot depend on compilers to implement it.

On the other hand, you can surely test it, and try to "coax" it into being optimized out, by (again) asking your compiler to do as much optimization as possible, for instance.

Upvotes: 4

Related Questions