]> git.ipfire.org Git - thirdparty/gcc.git/commit
PR libstdc++/91788 improve codegen for std::variant<T...>::index()
authorredi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 23 Sep 2019 15:54:16 +0000 (15:54 +0000)
committerredi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 23 Sep 2019 15:54:16 +0000 (15:54 +0000)
commitde61fb434a5d73f724a783e263a96634c657940b
treef2728a5feaa677602146900700f9c827342cfb1d
parent2805977f6719d7a487424dd2ded0b5d000cc5081
PR libstdc++/91788 improve codegen for std::variant<T...>::index()

If __index_type is a smaller type than size_t, then the result of
size_t(__index_type(-1)) is not equal to size_t(-1), but to an incorrect
value such as size_t(255) or size_t(65535). The old implementation of
variant<T...>::index() uses (size_t(__index_type(_M_index + 1)) - 1)
which is always correct, but generates suboptimal code for many common
cases.

When the __index_type is size_t or valueless variants are not possible
we can just return the value directly.

When the number of alternatives is sufficiently small the result of
converting the _M_index value to the corresponding signed type will be
either non-negative or -1. In those cases converting to the signed type
and then to size_t will either produce the correct positive value or
will sign extend -1 to (size_t)-1 as desired.

For the remaining case we keep the existing arithmetic operations to
ensure the correct result.

PR libstdc++/91788 (partial)
* include/std/variant (variant::index()): Improve codegen for cases
where conversion to size_t already works correctly.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@276056 138bc75d-0d04-0410-961f-82ee72b054a4
libstdc++-v3/ChangeLog
libstdc++-v3/include/std/variant