public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/111405] New: Problem with incorrect optimizion for "constexpr" function with possible overflow
@ 2023-09-13 15:23 3180104919 at zju dot edu.cn
  2023-09-13 15:27 ` [Bug c/111405] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: 3180104919 at zju dot edu.cn @ 2023-09-13 15:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111405
           Summary: Problem with incorrect optimizion for "constexpr"
                    function with possible overflow
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 3180104919 at zju dot edu.cn
  Target Milestone: ---

Created attachment 55891
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55891&action=edit
A demo file contains a funciton that will be wrongly optimized using -O2

I happened to find this problem when I did the CSAPP lab.

int isTmax(int x) {
  // make it all of 1
  // it's quite strange that the results of x + 1 + x and x + x + 1 are
different
  int c = x + x + 1;
  // check if it's all of 1
  int flag_all_ones = !(~c);
  // avoid -1
  int flag_not_neg1 = !!(x + 1);
  return flag_all_ones & flag_not_neg1;
}

This function will be incorrectly optimized to return zero only with "-O2"
compiler flag. But in fact isTmax(0x7fffffff) should return 1. Here's the
disassembly code using coredump:

000012ac <isTmax>:
  // check if it's all of 1
  int flag_all_ones = !(~c);
  // avoid -1
  int flag_not_neg1 = !!(x + 1);
  return flag_all_ones & flag_not_neg1;
}
    12ac:       b8 00 00 00 00          mov    $0x0,%eax
    12b1:       c3                      ret    

This function can be correctly compiled with no compiler optimization (-O0). 
And this behaviour always occurs using the latest 2 version gcc compiler (from
11.0 to 12.0). But using clang or msvc, everything works well. 

Thank you for your time.

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

* [Bug c/111405] Problem with incorrect optimizion for "constexpr" function with possible overflow
  2023-09-13 15:23 [Bug c/111405] New: Problem with incorrect optimizion for "constexpr" function with possible overflow 3180104919 at zju dot edu.cn
@ 2023-09-13 15:27 ` pinskia at gcc dot gnu.org
  2023-09-13 15:31 ` 3180104919 at zju dot edu.cn
  2023-09-14 12:54 ` xry111 at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-13 15:27 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Signed integer overflow is undefined behavior. 

Use -fwrapv or unsigned to do the addition to get the behavior you want.

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

* [Bug c/111405] Problem with incorrect optimizion for "constexpr" function with possible overflow
  2023-09-13 15:23 [Bug c/111405] New: Problem with incorrect optimizion for "constexpr" function with possible overflow 3180104919 at zju dot edu.cn
  2023-09-13 15:27 ` [Bug c/111405] " pinskia at gcc dot gnu.org
@ 2023-09-13 15:31 ` 3180104919 at zju dot edu.cn
  2023-09-14 12:54 ` xry111 at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: 3180104919 at zju dot edu.cn @ 2023-09-13 15:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Wang Chenyu <3180104919 at zju dot edu.cn> ---
(In reply to Andrew Pinski from comment #1)
> Signed integer overflow is undefined behavior. 
> 
> Use -fwrapv or unsigned to do the addition to get the behavior you want.

I see.. Thank you for your explanation

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

* [Bug c/111405] Problem with incorrect optimizion for "constexpr" function with possible overflow
  2023-09-13 15:23 [Bug c/111405] New: Problem with incorrect optimizion for "constexpr" function with possible overflow 3180104919 at zju dot edu.cn
  2023-09-13 15:27 ` [Bug c/111405] " pinskia at gcc dot gnu.org
  2023-09-13 15:31 ` 3180104919 at zju dot edu.cn
@ 2023-09-14 12:54 ` xry111 at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-09-14 12:54 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

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

--- Comment #3 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
I'm really amazed that in 2023 there are still textbooks teaching everyone to
invoke undefined behavior, even such a famous one.

And how's this related to constexpr?!

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

end of thread, other threads:[~2023-09-14 12:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-13 15:23 [Bug c/111405] New: Problem with incorrect optimizion for "constexpr" function with possible overflow 3180104919 at zju dot edu.cn
2023-09-13 15:27 ` [Bug c/111405] " pinskia at gcc dot gnu.org
2023-09-13 15:31 ` 3180104919 at zju dot edu.cn
2023-09-14 12:54 ` xry111 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).