Skip to content

Commit a347676

Browse files
committed
[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 b1339ab commit a347676

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

libcxx/include/vector

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,10 @@ private:
10271027
}
10281028

10291029
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
1030+
1031+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __add_alignment_assumption(pointer __p) _NOEXCEPT {
1032+
return static_cast<pointer>(__builtin_assume_aligned(__p, alignof(decltype(*__p))));
1033+
}
10301034
};
10311035

10321036
#if _LIBCPP_STD_VER >= 17
@@ -1418,25 +1422,25 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::assign(size_type __n
14181422
template <class _Tp, class _Allocator>
14191423
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
14201424
vector<_Tp, _Allocator>::begin() _NOEXCEPT {
1421-
return __make_iter(this->__begin_);
1425+
return __make_iter(__add_alignment_assumption(this->__begin_));
14221426
}
14231427

14241428
template <class _Tp, class _Allocator>
14251429
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::const_iterator
14261430
vector<_Tp, _Allocator>::begin() const _NOEXCEPT {
1427-
return __make_iter(this->__begin_);
1431+
return __make_iter(__add_alignment_assumption(this->__begin_));
14281432
}
14291433

14301434
template <class _Tp, class _Allocator>
14311435
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
14321436
vector<_Tp, _Allocator>::end() _NOEXCEPT {
1433-
return __make_iter(this->__end_);
1437+
return __make_iter(__add_alignment_assumption(this->__end_));
14341438
}
14351439

14361440
template <class _Tp, class _Allocator>
14371441
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::const_iterator
14381442
vector<_Tp, _Allocator>::end() const _NOEXCEPT {
1439-
return __make_iter(this->__end_);
1443+
return __make_iter(__add_alignment_assumption(this->__end_));
14401444
}
14411445

14421446
template <class _Tp, class _Allocator>

0 commit comments

Comments
 (0)