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

Mix <mix at emboss dot co.nz> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mix at emboss dot co.nz

--- Comment #2 from Mix <mix at emboss dot co.nz> ---
Just ran into this as well - here's a slightly more compact test case.


#include <stdio.h>

struct bytes { unsigned char b[8]; };

__attribute__((noinline)) void set(unsigned long ctx)
{
  struct bytes a = __builtin_bit_cast(struct bytes, ctx);
  struct bytes b = a;
  *__builtin_bit_cast(int *, b) = 1;
}

int main(void)
{
  int x = 0;
  set((unsigned long)&x);
  printf("%d\n", x);
  return x != 1;
}


--- LLM triage follows ---

# Cause

`set()` copies its pointer parameter into a byte array with
`__builtin_bit_cast`, copies the array, and casts it back to a pointer. After
early optimization its body is:

```
a = VIEW_CONVERT_EXPR<struct bytes>(p_2(D));
_3 = MEM[(struct bytes *)&a];
*_3 = 1;
```

modref's escape analysis of the parameter
(`modref_eaf_analysis::analyze_ssa_name` in `ipa-modref.cc`) examines the first
statement and records nothing. That statement stores the parameter into memory,
but its right-hand side is `VIEW_CONVERT_EXPR(p)`, not `p` itself. So it does
not match the "`*lhs = name`" case ("ssa name saved to memory"), which would
clear every flag. `-fdump-tree-modref1` shows:

```
Analyzing stmt: a = VIEW_CONVERT_EXPR<struct bytes>(p_2(D));
...
parm 0 flags: no_direct_clobber no_indirect_clobber no_direct_escape
no_indirect_escape
  no_direct_read no_indirect_read
```

The pointer then gets out through `a`. In `main()`, those flags tell the caller
that `set(&x)` neither reads nor clobbers `x`:

- `fre1` folds the load of `x` after the call to `0`: `printf ("%d\n", 0)`;
- `dse1` deletes `x = 0` as a dead store.

# C++ compilation requirement

**Plain C doesn't get there.** I tried a union, a GNU cast to a union, a vector
cast, byte-copy loops, and `memcpy` from `&p` into a struct, a union or an
array. Every one is folded to `*p = 1` before modref runs, and modref handles
that correctly:
- These forms lower to a store of the pointer as a scalar (`MEM<long>[&a] =
p`), which FRE forwards straight to the later load.
- The bit cast produces an aggregate store, which FRE doesn't look through.
That's why it survives in the C++ version.
- I know of no GCC pass that creates an aggregate `VIEW_CONVERT_EXPR` store
from C source. In the front ends, it comes from C++'s `__builtin_bit_cast` and,
I believe, from Fortran's `TRANSFER` and Ada's `Unchecked_Conversion`; I
haven't tried those.

**C with a GIMPLE function does reproduce it.** GCC's C front end can compile a
function written directly in GIMPLE (`-fgimple`), which is how GCC's own
testsuite targets single passes. Starting it at the modref pass keeps the early
passes from folding it away:

```c
__attribute__((noinline)) void __GIMPLE (ssa, startwith ("modref"))
set (int *p)
{
  struct bytes a;
  int * _2;

  __BB(2):
  a = __VIEW_CONVERT <struct bytes> (p_1(D));
  _2 = __MEM <int *> ((struct bytes *)&a);
  __MEM <int> (_2) = 1;
  return;
}
/* main() as before, plain C */
```

Reply via email to