Bug 114206 - [14/15/16/17 Regression] recursive function call vs local variable addresses since r0-92161
Summary: [14/15/16/17 Regression] recursive function call vs local variable addresses ...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 14.0
: P2 normal
Target Milestone: 14.5
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2024-03-02 09:06 UTC by congli
Modified: 2026-11-09 07:23 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.5.3
Known to fail: 4.6.3, 4.7.3, 5.1.0
Last reconfirmed: 2024-03-04 00:00:00


Attachments
slightly more complex testcase (211 bytes, text/plain)
2025-03-26 00:40 UTC, Drea Pinski
Details
Simplier testcase but kinda of more complex case to handle (225 bytes, text/plain)
2025-03-26 04:18 UTC, Drea Pinski
Details

Note You need to log in before you can comment on or make changes to this bug.
Description congli 2024-03-02 09:06:24 UTC
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
```
Comment 1 Drea Pinski 2024-03-02 09:13:56 UTC
I think both 0 and 1 are correct here.

The question becomes does the address of b need to be different between different calls of f. I am not 100% convinced it needs to be different.
Comment 2 congli 2024-03-02 09:27:14 UTC
That's correct. But I think it is not that reasonable if we treat the `b` like `b` is a `static const` variable rather than a `const` variable? Any documents telling this?
Comment 3 congli 2024-03-02 09:36:34 UTC
How about this one: https://gcc.godbolt.org/z/Wvhddb7nf?

We ensured the two `b`s are different at each f() call.
Comment 4 Xi Ruoyao 2024-03-02 11:06:38 UTC
(In reply to Andrew Pinski from comment #1)
> I think both 0 and 1 are correct here.
> 
> The question becomes does the address of b need to be different between
> different calls of f. I am not 100% convinced it needs to be different.

It looks like they needs to be different as they refer different objects and the lifetime of both object has still not ended when comparing them.
Comment 5 Arsen Arsenović 2024-03-02 15:15:28 UTC
(In reply to Xi Ruoyao from comment #4)
> It looks like they needs to be different as they refer different objects and
> the lifetime of both object has still not ended when comparing them.
this seems the case to me also
Comment 6 Richard Biener 2024-03-04 09:54:56 UTC
So we simplify this and RTL expand from

int f (int t, const int * a)
{
  const int b[4];
  _Bool _1;
  int _8;

  <bb 2> [local count: 1073741824]:
  if (t_3(D) == 0)
    goto <bb 3>; [49.25%]
  else
    goto <bb 4>; [50.75%]

  <bb 3> [local count: 528857912]:

  <bb 4> [local count: 1073741824]:
  # a_11 = PHI <a_5(D)(2), &b(3)>
  _1 = &b == a_11;
  _8 = (int) _1;
  b ={v} {CLOBBER(eos)};
  return _8;

We apply tail-recursion optimization here which coalescs both slots which
are used just by their address.  IIRC tail-recursion analysis uses alias
analysis to be able to handle some cases where TREE_ADDRESSABLE vars are
passed.

We miss considering variables live that are passed by reference (but otherwise
are "unused").

As you needed to cut off quite some optimizations to early simplify the
b == a compare this is a bit academic or needs much more obfuscation of the
compare to actually matter.

But yes, it's a bug.
Comment 7 Jakub Jelinek 2024-03-05 12:14:11 UTC
Started with r0-92161-g33977f81620a3e495be87d0381544f8ad26b2782
Comment 8 Richard Biener 2024-07-19 13:23:59 UTC
GCC 11 branch is being closed.
Comment 9 Drea Pinski 2025-03-26 00:40:19 UTC
Created attachment 60885 [details]
slightly more complex testcase

Still fails with `-Os -fno-tree-ccp -fno-tree-copy-prop -fno-tree-forwprop -fno-tree-fre -fno-tree-vrp` . But it shows that simply looking at arguments to see if what passes is &b will also fail.

I wonder if the life range analysis that is in cfgexpand should be shared here.
Comment 10 Drea Pinski 2025-03-26 04:18:50 UTC
Created attachment 60886 [details]
Simplier testcase but kinda of more complex case to handle

This one also fails at -Os. GCC knows that &b[0] does not escape via g but then tail call happens.
Comment 11 Richard Biener 2025-07-11 09:18:46 UTC
GCC 12 branch is being closed.
Comment 12 Jakub Jelinek 2026-09-11 10:49:37 UTC
GCC 13 branch is being closed.