> gcc does not reduce to call result if called function is not static in > -O2 (will do with -O2) > clang and msvc does it also in -O2 regardless of the function beeing > static or not > > can someone explain to me why the -O2 optimizer is not able(allowed) to > reduce this small sample the same way as clang/msvc?
GCC is optimizing for size here, so it is more careful with inlning. This is because it knows that function main is called once. If you rename main to something else or add a loop around the call, then it will inline for speed and do same work as clang. Clang (and probaby msvc) does not implement the heuristics that certain functions (static constructors, destructors, main and noreturns) are called once so they probably both optimize for speed. Even when optimizing for size it would be good idea to inline. However the inliner heruistics predicts that it is not. This is because at the inlining time compiler does not see that calee will optimize to constant. The reason is that you store the temporary vlues to array and those are not tracked. If you used scalar variables it would be able to constant fold everything early. Handling this would require either recovering early ipa-sra or adding return functions for values passed by reference. Honza