Re: Optimization attributes const vs. pure

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 30.05.2012 00:44, Ian Lance Taylor wrote:

> GCC does not know that the function fprintf does not modify global
> memory.  In fact, the function does modify global memory, in that it
> will change stderr structure.  For example, for all GCC knows, the
> function intcmp could call ferror(stderr)--that would be permitted by a
> pure function.  And that means that the call can not be hoisted out of
> the loop.

That does explain why my original variant isn't optimized, but it does
not explain why Ángel's modified variant (with a deferred printf() call)
also exhibits the same phaenomenon. This is the modified variant of his:

#include <stdio.h>
#include <string.h>
#include "int.h"
int main() {
    struct mi a, b;
    int vals[100];
    memset(&a, 0, sizeof(struct mi));
    memset(&b, 0, sizeof(struct mi));
    for (unsigned int i = 0; i < 100; i++) {
        vals[i] = intcmp(&a, &b);
    }
    for (unsigned int i = 0; i < 100; i++) {
        fprintf(stderr, "%d\n", vals[i]);
    }
    return 0;
}

Here, there's definitely no modified global memory and yet intcmp() is
still called every time in the loop.

Best regards,
Johannes


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux