Skip to content

Commit 94c2ede

Browse files
fhahnldionne
authored andcommitted
[libc++] Add assumption for align of begin and end pointers of vector.
Missing information about begin and end pointers of std::vector can lead to missed optimizations in LLVM. See llvm#101372 for a discussion of missed range check optimizations in hardened mode. Once llvm#108958 lands, the created `llvm.assume` calls for the alignment should be folded into the `load` instructions, resulting in no extra instructions after InstCombine.
1 parent b5ba4f0 commit 94c2ede

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

libcxx/include/__vector/vector.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,17 @@ class _LIBCPP_TEMPLATE_VIS vector {
341341
//
342342
// Iterators
343343
//
344-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __make_iter(this->__begin_); }
344+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
345+
return __make_iter(__add_alignment_assumption(this->__begin_));
346+
}
345347
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
346-
return __make_iter(this->__begin_);
348+
return __make_iter(__add_alignment_assumption(this->__begin_));
349+
}
350+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
351+
return __make_iter(__add_alignment_assumption(this->__end_));
347352
}
348-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __make_iter(this->__end_); }
349353
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
350-
return __make_iter(this->__end_);
354+
return __make_iter(__add_alignment_assumption(this->__end_));
351355
}
352356

353357
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
@@ -775,6 +779,10 @@ class _LIBCPP_TEMPLATE_VIS vector {
775779
}
776780

777781
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
782+
783+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __add_alignment_assumption(pointer __p) const _NOEXCEPT {
784+
return static_cast<pointer>(__builtin_assume_aligned(__p, alignof(decltype(*__p))));
785+
}
778786
};
779787

780788
#if _LIBCPP_STD_VER >= 17

0 commit comments

Comments
 (0)