This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Optimizing away deletion of null pointers with g++


On 08/16/2017 08:44 AM, Jonathan Wakely wrote:
> On 16 August 2017 at 15:40, Jonathan Wakely wrote:
>> On 16 August 2017 at 15:27, Oleg Endo wrote:
>>> On Wed, 2017-08-16 at 13:30 +0200, Paolo Carlini wrote:
>>>>
>>>> I didn't understand why we don't already handle the easy case:
>>>>
>>>> constexpr int* ptr = nullptr;
>>>> delete ptr;
>>>>
>>>
>>> What about overriding the global delete operator with some user defined
>>> implementation?  Is there something in the C++ standard that says the
>>> invocation can be completely omitted, i.e. on which side of the call
>>> the nullptr check is being done?
>>>
>>> One possible use case could be overriding the global delete operator to
>>> count the number of invocations, incl. for nullptr.  Not sure how
>>> useful that is though.
>>
>> Users can replace the deallocation function "operator delete" but this
>> is the delete operator ... a subtly different thing.
>>
>> Anyway, the standard says:
>>
>> "If the value of the operand of the delete-expression is a null
>> pointer value, it is unspecified whether a deallocation function will
>> be called as described above."
>>
>> So it's permitted to omit the call to operator delete.
> 
> Before C++11 the call was required:
> 
> "The delete-expression will call a deallocation function (3.7.3.2)."
> 
> 
> This was changed by
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#348
Which I think argues that we can safely remove a call to operator delete
when we know the pointer is null.  However, we can not assume that an
object passed to operator delete is non-null.

ISTM this would be better implemented in the optimizers rather than in
the front-end.  tree-ssa-dce.c would seem fairly natural.

The only wrinkle is we can't do it in C++03 or earlier mode.
jeff


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]