public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/95769] New: Constant expression in inline function not optimized
@ 2020-06-19 15:33 gcc at mailinator dot com
  2020-06-19 15:38 ` [Bug tree-optimization/95769] " jakub at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gcc at mailinator dot com @ 2020-06-19 15:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95769

            Bug ID: 95769
           Summary: Constant expression in inline function not optimized
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at mailinator dot com
  Target Milestone: ---

Godbolt link: https://godbolt.org/z/Kimna8

Code:

```
int constexpr expensive_function(int x){
    int result{};
    while(x!=1){
        x=x%2!=0 ? x*3+1 : x/2;
        result++;
    }
    return result;
}

int constexpr other_function(int a, int b){
    return a*expensive_function(b);
}

int f_(int x){
    return other_function(x, 1000);
}

int g(int x){
    return x*expensive_function(1000);
}
```

The function `g` is optimized so `expensive_function(1000)` is evaluated at
compile time. The function `f` isn't.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug tree-optimization/95769] Constant expression in inline function not optimized
  2020-06-19 15:33 [Bug tree-optimization/95769] New: Constant expression in inline function not optimized gcc at mailinator dot com
@ 2020-06-19 15:38 ` jakub at gcc dot gnu.org
  2020-06-19 15:45 ` gcc at mailinator dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-06-19 15:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95769

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
If you want to ensure a function is evaluated at compile time, it needs to be
either C++20 consteval, or you need to evaluate it in constant expression
context, e.g. constexpr auto x = expensive_function(10000);

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug tree-optimization/95769] Constant expression in inline function not optimized
  2020-06-19 15:33 [Bug tree-optimization/95769] New: Constant expression in inline function not optimized gcc at mailinator dot com
  2020-06-19 15:38 ` [Bug tree-optimization/95769] " jakub at gcc dot gnu.org
@ 2020-06-19 15:45 ` gcc at mailinator dot com
  2020-06-19 15:50 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: gcc at mailinator dot com @ 2020-06-19 15:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95769

--- Comment #2 from John Simon <gcc at mailinator dot com> ---
(In reply to Jakub Jelinek from comment #1)
> If you want to ensure a function is evaluated at compile time, it needs to
> be either C++20 consteval, or you need to evaluate it in constant expression
> context, e.g. constexpr auto x = expensive_function(10000);

in this case, doing that will make the `other_function` consteval too, so I
can't call it with non-constant values.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug tree-optimization/95769] Constant expression in inline function not optimized
  2020-06-19 15:33 [Bug tree-optimization/95769] New: Constant expression in inline function not optimized gcc at mailinator dot com
  2020-06-19 15:38 ` [Bug tree-optimization/95769] " jakub at gcc dot gnu.org
  2020-06-19 15:45 ` gcc at mailinator dot com
@ 2020-06-19 15:50 ` jakub at gcc dot gnu.org
  2020-06-19 15:54 ` jakub at gcc dot gnu.org
  2020-06-22  7:45 ` [Bug ipa/95769] " jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-06-19 15:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95769

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to John Simon from comment #2)
> (In reply to Jakub Jelinek from comment #1)
> > If you want to ensure a function is evaluated at compile time, it needs to
> > be either C++20 consteval, or you need to evaluate it in constant expression
> > context, e.g. constexpr auto x = expensive_function(10000);
> 
> in this case, doing that will make the `other_function` consteval too, so I
> can't call it with non-constant values.

So you can't make it consteval then, but you can still assign it into const
variable (or even constinit in C++20 to abort compilation if not evaluated into
constant).  Without that, the constexpr means nothing, the compiler might or
might not evaluate it at compile time.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug tree-optimization/95769] Constant expression in inline function not optimized
  2020-06-19 15:33 [Bug tree-optimization/95769] New: Constant expression in inline function not optimized gcc at mailinator dot com
                   ` (2 preceding siblings ...)
  2020-06-19 15:50 ` jakub at gcc dot gnu.org
@ 2020-06-19 15:54 ` jakub at gcc dot gnu.org
  2020-06-22  7:45 ` [Bug ipa/95769] " jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-06-19 15:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95769

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
And, in the compiler the C++ constant evaluation is something done only in the
C++ FE, while you are looking for IPA constant propagation and based on that
performing the C++ FE constant evaluation because that function had constexpr.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug ipa/95769] Constant expression in inline function not optimized
  2020-06-19 15:33 [Bug tree-optimization/95769] New: Constant expression in inline function not optimized gcc at mailinator dot com
                   ` (3 preceding siblings ...)
  2020-06-19 15:54 ` jakub at gcc dot gnu.org
@ 2020-06-22  7:45 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-06-22  7:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95769

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
If we are going to optimize this at IPA time, I'll note we'd need a separate
middle-end IL evaluation code, because the FE one isn't usable for this (not
even through a langhook), because it relies on the IL being unfolded FE trees
and has all the language imposed restrictions etc.
I think I've mentioned IL evaluation as something useful for loops to be able
to evaluate all their side-effects and judge if they are small enough (not too
much overwritten memory, etc.) to replace the original loop, and if we have it,
using it for functions to whose arguments we propagate constants, perhaps using
the constexpr as a hint to try somewhat longer, could work.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-06-22  7:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-19 15:33 [Bug tree-optimization/95769] New: Constant expression in inline function not optimized gcc at mailinator dot com
2020-06-19 15:38 ` [Bug tree-optimization/95769] " jakub at gcc dot gnu.org
2020-06-19 15:45 ` gcc at mailinator dot com
2020-06-19 15:50 ` jakub at gcc dot gnu.org
2020-06-19 15:54 ` jakub at gcc dot gnu.org
2020-06-22  7:45 ` [Bug ipa/95769] " jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).