Unlike GCC, clang will iterate gvn (what GCC calls fre, because in GCC, gvn is properly split into an analysis, and fre uses it to do full redundancy elimination) until it stops changing, and does so fairly early in the pipeline
I wrote fre for GCC (and working on llvm's next gvn) and long story short, GCC is more powerful, but it doesn't always run early enough, and is not iterated.
The general problem of detecting all herbrand equivalences is exponential time, even without trying to evaluate the code. So you can't win all the time anyway.
This surprises me. This makes it sound like inlining and vectorizztion are performed independently of constant folding. It seems like the compiler should be able to recognize when a function depends only on its inputs. Then constant folding should be able to inline / evaluate any code that can be compile-time evaluated, no matter how much code that is. It doesn't seem like this should be left up to chance.
Is there no way to indicate to GCC when you want something fully evaluated at compile-time? Maybe some pragmas? 98% of the time I probably don't care whether GCC does a good job optimizing this properly, but for the times I do care: what should I do?
c++ has constexpr for this. a constexpr expression will be evaluated at compile time if the result is used in a constant expression (i.e. assignd to an enum constant or a constexpr variable), or an error will be generated if it is not possible to do so (the expression does not fulfill the requirements to be constexpr).
https://godbolt.org/g/Uvo7iO