[cfe-commits] [libcxx] r150542 - in /libcxx/trunk/include: __split_buffer memory vector

Howard Hinnant hhinnant at apple.com
Wed Feb 15 07:03:38 PST 2012


On Feb 15, 2012, at 1:19 AM, Dave Zarzycki wrote:

> On Feb 14, 2012, at 7:41 PM, Howard Hinnant <hhinnant at apple.com> wrote:
> 
>> void
>> __split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
>> {
>> -    while (__begin_ < __new_begin)
>> +    while (__begin_ != __new_begin)
>>        __alloc_traits::destroy(__alloc(), __begin_++);
>> }
> 
> Howard,
> 
> Are there other places in libcxx where we should replace relational pointer comparisons with equality pointer comparisons?

Almost certainly, but I have not found those places yet.

> Also, what does the latest C++ specification say about what compilers may or may not do with relational pointer comparisons? What does clang do? I've been searching the web and I don't feel like I've found a solid answer yet.

I don't believe the C++ standard will say anything.

I believe what is happening here is the loop:

   while (__begin_ != __new_begin)
       __alloc_traits::destroy(__alloc(), __begin_++);

When __alloc_traits::destroy is a no-op, is being transformed into:

     __begin_ = __new_begin;

on the rationale that if the loop is an infinite loop, then we've got undefined behavior, and the only way for the loop to exit is when __begin_ == __new_begin.

With

    while (__begin_ < __new_begin)

The loop can exit with __begin_ >= __new_begin, and the compiler can't tell that it will always be the case that __begin_ == __new_begin on loop exit.

Disclaimer:  This is all speculation on my part.  I know next to nothing about compiler optimizations.

Howard




More information about the cfe-commits mailing list