public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/114206] New: GCC generates wrong-code
@ 2024-03-02  9:06 congli at smail dot nju.edu.cn
  2024-03-02  9:13 ` [Bug tree-optimization/114206] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: congli at smail dot nju.edu.cn @ 2024-03-02  9:06 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114206
           Summary: GCC generates wrong-code
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: congli at smail dot nju.edu.cn
  Target Milestone: ---

The program shown below presents a wrong code bug, where the correct results
should be "f(0, NULL) = 0" while `-Os -fno-tree-ccp -fno-tree-copy-prop
-fno-tree-forwprop -fno-tree-fre -fno-tree-vrp` prints "f(0, NULL) = 1".

```
#include <stdio.h>

int f(int t, const int *a) {
  const int b[4] = {0};

  if (t == 0) {
    return f(1, b);
  } else {
    return b == a;
  }
}

int main(void) {
  printf("f(0, NULL) = %d\n", f(0, NULL));
}
```

Compiler Explorer: https://gcc.godbolt.org/z/W164xWMrP 

We checked the assembly, finding that it is weird that the compiler generates a
`cmove` instruction. See explanations below:

```
f:
        leaq    -16(%rsp), %rax -> RAX = RSP-16
        testl   %edi, %edi      -> we called f(0, NULL); %edi = 0, ZF = 1
        cmove   %rax, %rsi      -> condition fulfilled; RSI=RAX=RSP-16; weird
generation
        cmpq    %rax, %rsi      -> RSI=RAX; ZF=1
        sete    %al             -> AL = 1
        movzbl  %al, %eax       -> EAX = 1 (error)
        ret
```

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

end of thread, other threads:[~2024-03-05 12:14 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-02  9:06 [Bug c/114206] New: GCC generates wrong-code congli at smail dot nju.edu.cn
2024-03-02  9:13 ` [Bug tree-optimization/114206] " pinskia at gcc dot gnu.org
2024-03-02  9:27 ` [Bug tree-optimization/114206] recursive function call vs local variable addresses congli at smail dot nju.edu.cn
2024-03-02  9:36 ` congli at smail dot nju.edu.cn
2024-03-02 11:06 ` xry111 at gcc dot gnu.org
2024-03-02 15:15 ` arsen at gcc dot gnu.org
2024-03-04  9:54 ` rguenth at gcc dot gnu.org
2024-03-04 17:31 ` [Bug tree-optimization/114206] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
2024-03-05 12:14 ` 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).