malloc cannot alias preexisting pointers

Martin Sebor msebor@gmail.com
Mon May 13 18:33:00 GMT 2019


On 5/13/19 11:37 AM, Marc Glisse wrote:
> On Mon, 13 May 2019, Martin Sebor wrote:
> 
>> On 5/11/19 5:33 PM, Marc Glisse wrote:
>>> Hello,
>>>
>>> this patch lets gcc know that if a pointer existed before the call to 
>>> malloc, the result of malloc cannot alias it. This is a bit ad hoc 
>>> and fragile. A small improvement would be, when the 2 statements are 
>>> in the same bb but in the wrong order, to check if there is any 
>>> statement in between that might prevent from reordering them. But 
>>> that's more complicated, and the patch as it is already does help.
>>
>> I.e., given the description of attribute malloc:
>>
>>  the pointer P returned by the function cannot alias any other
>>  pointer valid when the function returns, and moreover no pointers
>>  to valid objects occur in any storage addressed by P.
>>
>> doesn't the same guarantee hold for other functions declared with
>> the attribute (so that the same optimization could be applied to
>> them as well)?
> 
> The patch tests DECL_IS_MALLOC, not BUILT_IN_MALLOC. From the failures I 
> got with earlier versions, that seems to include alloca at least, so I 
> would expect it also includes any function with the attribute.

Very good.  May I suggest to add a test case to verify that?

Martin



More information about the Gcc-patches mailing list