__builtin_assume removes calls to functions with side effect


[Please CC me when replying, I'm not on the list]

I've been experimenting with replacing assert with __builtin_assume in
release builds, and I've hit a problem. I did not open a bug because I'm
not sure if this is really a bug, or just the documentation is not clear
enough. Anyway, look at this simple program:

#include <stdio.h>

int global;

int foo()
    global = 1;
    return 3;

int main()
    int x = foo();
    __builtin_assume(x == 3);
    printf("%d %d\n", x, global);
    return 0;

If I try to run it:
$ clang++ -O2 foo.cpp && ./a.out
3 0

(If I compile with -O0, I get the expected "3 1" as output). The
documentation itself says "The argument itself is never evaluated, so
any side effects of the expression will be discarded.", but the argument
here is "x == 3", not "foo() == 3", so IMHO it shouldn't eliminate the
function call here. Am I right here?

I get the same result from clang 3.8.0 from gentoo and from the git master.


This looks like a bug to me… It seems that callers of SimplifyInstruction are not checking to see if the replaced instruction is side-effect free.

Should be fixed in r273711.

Yes, it really fixes it. Thanks.