Re: Correct way to express to the compiler "this does not get clobbered"?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 03/12/2020 12:28, Andrea Corallo wrote:
> Richard Earnshaw <Richard.Earnshaw@xxxxxxxxxxxx> writes:
> 
>> On 03/12/2020 11:47, Andrea Corallo via Gcc-help wrote:
>>> Hi all,
>>>
>>> I've a piece of code that reduced looks like this:
>>>
>>> #+begin_src C
>>> typedef struct {
>>>   void (*fun_ptr)(void);
>>> } x_t;
>>>
>>> x_t *x;
>>>
>>> void
>>> f (void)
>>> {
>>>   const x_t const *y = x;
>>>   for (int i = 0; i < 1000; ++i)
>>>     y->fun_ptr ();
>>> }
>>> #+end_src
>>>
>>> What is the correct way (if any) to express to the compiler that the
>>> value of y->fun_ptr does not get clobbered by the function call itself
>>> so the corresponding load to obtain its value can be moved out of the
>>> loop?
>>>
>>> My understanding is that the const qualifier is more for diagnostic
>>> reasons and is not sufficient for GCC to make this assumption.  OTOH I
>>> cannot give 'fun_ptr' the attribute pure as it's not.
>>>
>>> Thanks
>>>
>>>   Andrea
>>>
>>
>> Why not just put the function pointer in a local variable?  Then the
>> compiler will know that the value can't change.
> 
> Hi Richard,
> 
> yeah that would do the job.
> 
> This is generated code and changing the code generator that way might
> not be completely trivial (e.g. where is the best position to perform
> the assignments to each local variable?).  Therefore before going for
> this solution I'd like to be convinced there's no way to express this
> directly to GCC.
> 
>   Andrea
> 

Perhaps you could declare y with the restrict qualifier?  But that would
need to apply to all of *y, not just fun_ptr.

R.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux