Re: Question about builtin_free doesn't read memory

2021-11-28 Thread Bin.Cheng via Gcc
On Sun, Nov 28, 2021 at 4:11 PM Jan Hubicka  wrote:
>
> > Hi,
> > In function ref_maybe_used_by_call_p_1, there is below code snippet
> >  /* The following builtins do not read from memory.  */
> >  case BUILT_IN_FREE:
> >  ...
> >return false;
> >
> > I am confused because free function does read from (and even write to)
> > memory pointed to by passed argument?
>
> Free is a black box and makes the memory pointed to disappear without
> actually worrying what values it holds. We rely on fact that we do not
> see free imlementation and does not worry about the details of its
> implementation (whcih probably has sort of linked list before address
> ptr points to)
> > I am thinking DSE optimizations like:
> >   *ptr = value;
> >   free(ptr);
> >   *ptr = undef;
> > Does GCC take advantage of UB to eliminate the first store to ptr if
> > free is considered not reading memory?
>
> The aim here is to optimize out *ptr = value.
>
> Honza
Thanks very much for explaining.

Thanks,
bin


Re: Question about builtin_free doesn't read memory

2021-11-28 Thread Jan Hubicka via Gcc
> Hi,
> In function ref_maybe_used_by_call_p_1, there is below code snippet
>  /* The following builtins do not read from memory.  */
>  case BUILT_IN_FREE:
>  ...
>return false;
> 
> I am confused because free function does read from (and even write to)
> memory pointed to by passed argument?

Free is a black box and makes the memory pointed to disappear without
actually worrying what values it holds. We rely on fact that we do not
see free imlementation and does not worry about the details of its
implementation (whcih probably has sort of linked list before address
ptr points to)
> I am thinking DSE optimizations like:
>   *ptr = value;
>   free(ptr);
>   *ptr = undef;
> Does GCC take advantage of UB to eliminate the first store to ptr if
> free is considered not reading memory?

The aim here is to optimize out *ptr = value.

Honza
> 
> Thanks,
> bin


Question about builtin_free doesn't read memory

2021-11-27 Thread Bin.Cheng via Gcc
Hi,
In function ref_maybe_used_by_call_p_1, there is below code snippet
 /* The following builtins do not read from memory.  */
 case BUILT_IN_FREE:
 ...
   return false;

I am confused because free function does read from (and even write to)
memory pointed to by passed argument?
I am thinking DSE optimizations like:
  *ptr = value;
  free(ptr);
  *ptr = undef;
Does GCC take advantage of UB to eliminate the first store to ptr if
free is considered not reading memory?

Thanks,
bin