Skip to content

[libc++] Use __is_address_in_range in vector #139032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025

Conversation

philnik777
Copy link
Contributor

This avoids a branch in vectors code on whether we're constant evaluating, which improves our coverage of constant-evaluated code.

@philnik777 philnik777 marked this pull request as ready for review May 9, 2025 19:45
@philnik777 philnik777 requested a review from a team as a code owner May 9, 2025 19:45
@philnik777 philnik777 merged commit df78e28 into llvm:main May 9, 2025
87 checks passed
@philnik777 philnik777 deleted the vector_use_is_address_in_range branch May 9, 2025 19:45
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label May 9, 2025
@llvmbot
Copy link
Member

llvmbot commented May 9, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

This avoids a branch in vectors code on whether we're constant evaluating, which improves our coverage of constant-evaluated code.


Full diff: https://github.com/llvm/llvm-project/pull/139032.diff

1 Files Affected:

  • (modified) libcxx/include/__vector/vector.h (+2-3)
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index af3f7b974cdcf..12276296dd0a9 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -1254,8 +1254,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
 vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x) {
   pointer __p = this->__begin_ + (__position - begin());
   if (__n > 0) {
-    // We can't compare unrelated pointers inside constant expressions
-    if (!__libcpp_is_constant_evaluated() && __n <= static_cast<size_type>(this->__cap_ - this->__end_)) {
+    if (__n <= static_cast<size_type>(this->__cap_ - this->__end_)) {
       size_type __old_n  = __n;
       pointer __old_last = this->__end_;
       if (__n > static_cast<size_type>(this->__end_ - __p)) {
@@ -1266,7 +1265,7 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_
       if (__n > 0) {
         __move_range(__p, __old_last, __p + __old_n);
         const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
-        if (__p <= __xr && __xr < this->__end_)
+        if (std::__is_pointer_in_range(std::__to_address(__p), std::__to_address(__end_), std::addressof(__x)))
           __xr += __old_n;
         std::fill_n(__p, __n, *__xr);
       }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants