3 ms·
titan:/tmp geofft$ cc -O3 -c test.c Is your use of 'cc' instead of 'gcc' intentional here? It implies that all compilers make this "optimization", but I think
by nkurz 11y ago
titan:/tmp geofft$ cc -O3 -c test.c
Is your use of 'cc' instead of 'gcc' intentional here? It implies that all compilers make this "optimization", but I think it's currently only GCC. ICC, Clang, and MSVC continue to work that way that the programmer intended: Godbolt for ICC and Clang http://goo.gl/jFfvyz http://goo.gl/jFfvyz, MSVC http://webcompiler.cloudapp.net/ http://webcompiler.cloudapp.net/.
While it would be unwise to rely on this "polite" behavior from a compiler, I find it difficult to argue that GCC does a better job of compiling this particular code. But perhaps the speedup on similar code is enough to justify the occasional insidious bug?
It would be nice if there was a GCC option that produced a warning here. Is there one?
- geofft 11y agoNo, that's just habit and me being slightly sloppy because it's a forum comment (same with the code style, sorry). I did expect this to be common across multiple compilers, so I'm surprised it's only gcc. I'd be curious if there's a similar-in-spirit example for LLVM, since I do expect they do similar optimizations. Looks like gcc -Wstrict-overflow=3 or higher produces a warning. Alternatively, -fno-strict-overflow disables the optimization (which is included as part of -O2 or higher).