Re: Limits on constant folding

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

 



On 18/04/2021 01:55, NightStrike via Gcc-help wrote:
> I hope I'm using the term "constant folding" here correctly.  If not,
> I apologize.
> 
> Is there a limit to the amount of mathematical subexpressions that GCC
> can remove at compile time?
> 
> Assuming the following:
> 
> int x = 2 * 3;
> 
> Presumably with the right compiler options, that will compile as:
> 
> int x = 6;
> 
> But if that is more complicated:
> 
> int x = 2 * 3 * 4 / 6 / 7 + 8 - 9;
> 
> how far will GCC go before it hits some internal limit where it can no
> longer do the calculation at compile time?
> 

I think in cases like this, there it is limited by memory.  Some
analysis and compile-time calculations have limits (such as for
constexpr functions or nested templates in C++) which can be adjusted by
compiler command line options.  But usually you have to be doing
something /very/ strange to hit the limits.

You can test your code to see, using an online compiler such as:

<https://godbolt.org/z/jsojsePGj>

Look at the generated assembly - even if you are not familiar with
assembly, you should be able to see quickly if the results of the
expression are calculated at compile-time or run-time.  (Don't forget to
enable optimisation.)

You can also use "__builtin_constant_p(x)"  to determine if the compiler
sees "x" as a compile-time constant or not.





[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