diff --git a/llvm/cmake/config-ix.cmake b/llvm/cmake/config-ix.cmake index b4c54da01912..818fafbce148 100644 --- a/llvm/cmake/config-ix.cmake +++ b/llvm/cmake/config-ix.cmake @@ -351,6 +351,15 @@ else() unset(HAVE_FFI_CALL CACHE) endif( LLVM_ENABLE_FFI ) +# Whether we can use std::is_trivially_copyable to verify llvm::is_trivially_copyable. +CHECK_CXX_SOURCE_COMPILES(" +#include +struct T { int val; }; +static_assert(std::is_trivially_copyable::value, \"ok\"); +int main() { return 0;} +" HAVE_STD_IS_TRIVIALLY_COPYABLE) + + # Define LLVM_HAS_ATOMICS if gcc or MSVC atomic builtins are supported. include(CheckAtomic) diff --git a/llvm/docs/ProgrammersManual.rst b/llvm/docs/ProgrammersManual.rst index e303a7a18eba..d9925d69d9f6 100644 --- a/llvm/docs/ProgrammersManual.rst +++ b/llvm/docs/ProgrammersManual.rst @@ -1530,7 +1530,7 @@ SmallVector has grown a few other minor advantages over std::vector, causing #. std::vector is exception-safe, and some implementations have pessimizations that copy elements when SmallVector would move them. -#. SmallVector understands ``std::is_trivially_copyable`` and uses realloc aggressively. +#. SmallVector understands ``llvm::is_trivially_copyable`` and uses realloc aggressively. #. Many LLVM APIs take a SmallVectorImpl as an out parameter (see the note below). diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h index 7f7a4593ae36..42e4fc84175c 100644 --- a/llvm/include/llvm/ADT/DenseMap.h +++ b/llvm/include/llvm/ADT/DenseMap.h @@ -426,8 +426,8 @@ protected: setNumEntries(other.getNumEntries()); setNumTombstones(other.getNumTombstones()); - if (std::is_trivially_copyable::value && - std::is_trivially_copyable::value) + if (is_trivially_copyable::value && + is_trivially_copyable::value) memcpy(reinterpret_cast(getBuckets()), other.getBuckets(), getNumBuckets() * sizeof(BucketT)); else diff --git a/llvm/include/llvm/ADT/Optional.h b/llvm/include/llvm/ADT/Optional.h index 5fff0acca816..be32178cb185 100644 --- a/llvm/include/llvm/ADT/Optional.h +++ b/llvm/include/llvm/ADT/Optional.h @@ -17,10 +17,10 @@ #include "llvm/ADT/None.h" #include "llvm/Support/Compiler.h" +#include "llvm/Support/type_traits.h" #include #include #include -#include #include namespace llvm { @@ -32,7 +32,7 @@ namespace optional_detail { struct in_place_t {}; /// Storage for any type. -template ::value> +template ::value> class OptionalStorage { union { char empty; diff --git a/llvm/include/llvm/ADT/PointerIntPair.h b/llvm/include/llvm/ADT/PointerIntPair.h index 600fcebff3ea..cb8b202c48b7 100644 --- a/llvm/include/llvm/ADT/PointerIntPair.h +++ b/llvm/include/llvm/ADT/PointerIntPair.h @@ -15,6 +15,7 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/PointerLikeTypeTraits.h" +#include "llvm/Support/type_traits.h" #include #include #include @@ -126,6 +127,19 @@ public: } }; +// Specialize is_trivially_copyable to avoid limitation of llvm::is_trivially_copyable +// when compiled with gcc 4.9. +template +struct is_trivially_copyable> : std::true_type { +#ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE + static_assert(std::is_trivially_copyable>::value, + "inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable"); +#endif +}; + + template struct PointerIntPairInfo { static_assert(PtrTraits::NumLowBitsAvailable < diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 1d6faf6509f9..5a5d47b783c2 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -1428,7 +1428,7 @@ template // is trivially copyable. using sort_trivially_copyable = conjunction< std::is_pointer, - std::is_trivially_copyable::value_type>>; + is_trivially_copyable::value_type>>; } // namespace detail // Provide wrappers to std::sort which shuffle the elements before sorting diff --git a/llvm/include/llvm/Config/config.h.cmake b/llvm/include/llvm/Config/config.h.cmake index 4da1d199db67..6664ad335584 100644 --- a/llvm/include/llvm/Config/config.h.cmake +++ b/llvm/include/llvm/Config/config.h.cmake @@ -332,6 +332,9 @@ /* Define as the return type of signal handlers (`int' or `void'). */ #cmakedefine RETSIGTYPE ${RETSIGTYPE} +/* Define if std::is_trivially_copyable is supported */ +#cmakedefine HAVE_STD_IS_TRIVIALLY_COPYABLE ${HAVE_STD_IS_TRIVIALLY_COPYABLE} + /* Define to a function implementing stricmp */ #cmakedefine stricmp ${stricmp} diff --git a/llvm/include/llvm/DebugInfo/CodeView/TypeHashing.h b/llvm/include/llvm/DebugInfo/CodeView/TypeHashing.h index 9f34d026b1ba..e6ade770457c 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/TypeHashing.h +++ b/llvm/include/llvm/DebugInfo/CodeView/TypeHashing.h @@ -171,10 +171,15 @@ struct GloballyHashedType { return Hashes; } }; +#if defined(_MSC_VER) +// is_trivially_copyable is not available in older versions of libc++, but it is +// available in all supported versions of MSVC, so at least this gives us some +// coverage. static_assert(std::is_trivially_copyable::value, "GloballyHashedType must be trivially copyable so that we can " "reinterpret_cast arrays of hash data to arrays of " "GloballyHashedType"); +#endif } // namespace codeview template <> struct DenseMapInfo { diff --git a/llvm/include/llvm/Support/type_traits.h b/llvm/include/llvm/Support/type_traits.h index 383f087f4bc2..7b7d5d991f3f 100644 --- a/llvm/include/llvm/Support/type_traits.h +++ b/llvm/include/llvm/Support/type_traits.h @@ -85,6 +85,11 @@ template union move_construction_triviality_helper { ~move_construction_triviality_helper() = default; }; +template +union trivial_helper { + T t; +}; + } // end namespace detail /// An implementation of `std::is_trivially_copy_constructible` since we have @@ -109,6 +114,78 @@ struct is_trivially_move_constructible : std::true_type {}; template struct is_trivially_move_constructible : std::true_type {}; + +template +struct is_copy_assignable { + template + static auto get(F*) -> decltype(std::declval() = std::declval(), std::true_type{}); + static std::false_type get(...); + static constexpr bool value = decltype(get((T*)nullptr))::value; +}; + +template +struct is_move_assignable { + template + static auto get(F*) -> decltype(std::declval() = std::declval(), std::true_type{}); + static std::false_type get(...); + static constexpr bool value = decltype(get((T*)nullptr))::value; +}; + + +// An implementation of `std::is_trivially_copyable` since STL version +// is not equally supported by all compilers, especially GCC 4.9. +// Uniform implementation of this trait is important for ABI compatibility +// as it has an impact on SmallVector's ABI (among others). +template +class is_trivially_copyable { + + // copy constructors + static constexpr bool has_trivial_copy_constructor = + std::is_copy_constructible>::value; + static constexpr bool has_deleted_copy_constructor = + !std::is_copy_constructible::value; + + // move constructors + static constexpr bool has_trivial_move_constructor = + std::is_move_constructible>::value; + static constexpr bool has_deleted_move_constructor = + !std::is_move_constructible::value; + + // copy assign + static constexpr bool has_trivial_copy_assign = + is_copy_assignable>::value; + static constexpr bool has_deleted_copy_assign = + !is_copy_assignable::value; + + // move assign + static constexpr bool has_trivial_move_assign = + is_move_assignable>::value; + static constexpr bool has_deleted_move_assign = + !is_move_assignable::value; + + // destructor + static constexpr bool has_trivial_destructor = + std::is_destructible>::value; + + public: + + static constexpr bool value = + has_trivial_destructor && + (has_deleted_move_assign || has_trivial_move_assign) && + (has_deleted_move_constructor || has_trivial_move_constructor) && + (has_deleted_copy_assign || has_trivial_copy_assign) && + (has_deleted_copy_constructor || has_trivial_copy_constructor); + +#ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE + static_assert(value == std::is_trivially_copyable::value, + "inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable"); +#endif +}; +template +class is_trivially_copyable : public std::true_type { +}; + + } // end namespace llvm #endif // LLVM_SUPPORT_TYPE_TRAITS_H diff --git a/llvm/tools/llvm-diff/DifferenceEngine.cpp b/llvm/tools/llvm-diff/DifferenceEngine.cpp index 64c0dc61e806..2cf1afbc6af5 100644 --- a/llvm/tools/llvm-diff/DifferenceEngine.cpp +++ b/llvm/tools/llvm-diff/DifferenceEngine.cpp @@ -67,7 +67,7 @@ public: unsigned NewSize = Storage.size() - 1; if (NewSize) { // Move the slot at the end to the beginning. - if (std::is_trivially_copyable::value) + if (is_trivially_copyable::value) Storage[0] = Storage[NewSize]; else std::swap(Storage[0], Storage[NewSize]); diff --git a/llvm/unittests/ADT/ArrayRefTest.cpp b/llvm/unittests/ADT/ArrayRefTest.cpp index f3da4c675a85..4690319ae52e 100644 --- a/llvm/unittests/ADT/ArrayRefTest.cpp +++ b/llvm/unittests/ADT/ArrayRefTest.cpp @@ -262,7 +262,7 @@ TEST(ArrayRefTest, makeArrayRefFromStdArray) { } } -static_assert(std::is_trivially_copyable>::value, +static_assert(is_trivially_copyable>::value, "trivially copyable"); } // end anonymous namespace diff --git a/llvm/unittests/ADT/ImmutableListTest.cpp b/llvm/unittests/ADT/ImmutableListTest.cpp index ab3b8b472b90..b0b1e0e6c29f 100644 --- a/llvm/unittests/ADT/ImmutableListTest.cpp +++ b/llvm/unittests/ADT/ImmutableListTest.cpp @@ -267,7 +267,7 @@ TEST_F(ImmutableListTest, LongListOrderingTest) { ASSERT_EQ(6, i); } -static_assert(std::is_trivially_copyable>>::value, +static_assert(is_trivially_copyable>>::value, "trivially copyable"); } // namespace diff --git a/llvm/unittests/ADT/OptionalTest.cpp b/llvm/unittests/ADT/OptionalTest.cpp index 7506453c4d90..249c9268bcfd 100644 --- a/llvm/unittests/ADT/OptionalTest.cpp +++ b/llvm/unittests/ADT/OptionalTest.cpp @@ -17,10 +17,10 @@ using namespace llvm; -static_assert(std::is_trivially_copyable>::value, - "trivially copyable"); +static_assert(is_trivially_copyable>::value, + "trivially copyable"); -static_assert(std::is_trivially_copyable>>::value, +static_assert(is_trivially_copyable>>::value, "trivially copyable"); void OptionalWorksInConstexpr() { @@ -66,8 +66,8 @@ unsigned NonDefaultConstructible::Destructions = 0; unsigned NonDefaultConstructible::CopyAssignments = 0; static_assert( - !std::is_trivially_copyable>::value, - "not trivially copyable"); + !is_trivially_copyable>::value, + "not trivially copyable"); // Test fixture class OptionalTest : public testing::Test { @@ -227,8 +227,9 @@ struct MultiArgConstructor { }; unsigned MultiArgConstructor::Destructions = 0; -static_assert(!std::is_trivially_copyable>::value, - "not trivially copyable"); +static_assert( + !is_trivially_copyable>::value, + "not trivially copyable"); TEST_F(OptionalTest, Emplace) { MultiArgConstructor::ResetCounts(); @@ -277,7 +278,7 @@ unsigned MoveOnly::MoveConstructions = 0; unsigned MoveOnly::Destructions = 0; unsigned MoveOnly::MoveAssignments = 0; -static_assert(!std::is_trivially_copyable>::value, +static_assert(!is_trivially_copyable>::value, "not trivially copyable"); TEST_F(OptionalTest, MoveOnlyNull) { @@ -381,7 +382,7 @@ private: unsigned Immovable::Constructions = 0; unsigned Immovable::Destructions = 0; -static_assert(!std::is_trivially_copyable>::value, +static_assert(!is_trivially_copyable>::value, "not trivially copyable"); TEST_F(OptionalTest, ImmovableEmplace) { diff --git a/llvm/unittests/ADT/PointerIntPairTest.cpp b/llvm/unittests/ADT/PointerIntPairTest.cpp index 8a42e5b9f557..b8ba3e32b288 100644 --- a/llvm/unittests/ADT/PointerIntPairTest.cpp +++ b/llvm/unittests/ADT/PointerIntPairTest.cpp @@ -62,7 +62,7 @@ TEST(PointerIntPairTest, GetSet) { EXPECT_EQ(&s, Pair2.getPointer()); EXPECT_EQ(E::Case3, Pair2.getInt()); - static_assert(std::is_trivially_copyable>::value, + static_assert(is_trivially_copyable>::value, "trivially copyable"); } @@ -101,7 +101,7 @@ TEST(PointerIntPairTest, ManyUnusedBits) { (int)PointerLikeTypeTraits::NumLowBitsAvailable); static_assert( - std::is_trivially_copyable< + is_trivially_copyable< PointerIntPair>::value, "trivially copyable"); } diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp index 50e38c50f621..fbf2d8422a44 100644 --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -1087,7 +1087,6 @@ TEST(StringRefTest, GTestPrinter) { EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo"))); } -static_assert(std::is_trivially_copyable::value, - "trivially copyable"); +static_assert(is_trivially_copyable::value, "trivially copyable"); } // end anonymous namespace diff --git a/llvm/unittests/Analysis/BlockFrequencyInfoTest.cpp b/llvm/unittests/Analysis/BlockFrequencyInfoTest.cpp index 5dd399517164..2aeba947a745 100644 --- a/llvm/unittests/Analysis/BlockFrequencyInfoTest.cpp +++ b/llvm/unittests/Analysis/BlockFrequencyInfoTest.cpp @@ -91,7 +91,7 @@ TEST_F(BlockFrequencyInfoTest, Basic) { EXPECT_EQ(BFI.getBlockFreq(BB3).getFrequency(), BB3Freq); } -static_assert(std::is_trivially_copyable::value, +static_assert(is_trivially_copyable::value, "trivially copyable"); } // end anonymous namespace diff --git a/llvm/unittests/Bitstream/BitstreamReaderTest.cpp b/llvm/unittests/Bitstream/BitstreamReaderTest.cpp index 669288e42647..f58af220f2d1 100644 --- a/llvm/unittests/Bitstream/BitstreamReaderTest.cpp +++ b/llvm/unittests/Bitstream/BitstreamReaderTest.cpp @@ -161,7 +161,7 @@ TEST(BitstreamReaderTest, shortRead) { } } -static_assert(std::is_trivially_copyable::value, +static_assert(is_trivially_copyable::value, "trivially copyable"); } // end anonymous namespace diff --git a/llvm/unittests/CodeGen/MachineInstrTest.cpp b/llvm/unittests/CodeGen/MachineInstrTest.cpp index 7c9faeca829b..33baaf62efdf 100644 --- a/llvm/unittests/CodeGen/MachineInstrTest.cpp +++ b/llvm/unittests/CodeGen/MachineInstrTest.cpp @@ -383,7 +383,6 @@ TEST(MachineInstrExtraInfo, RemoveExtraInfo) { ASSERT_FALSE(MI->getHeapAllocMarker()); } -static_assert(std::is_trivially_copyable::value, - "trivially copyable"); +static_assert(is_trivially_copyable::value, "trivially copyable"); } // end namespace diff --git a/llvm/unittests/CodeGen/TypeTraitsTest.cpp b/llvm/unittests/CodeGen/TypeTraitsTest.cpp index 7287ac363ba6..840375bd4ab5 100644 --- a/llvm/unittests/CodeGen/TypeTraitsTest.cpp +++ b/llvm/unittests/CodeGen/TypeTraitsTest.cpp @@ -12,15 +12,14 @@ #include "llvm/CodeGen/SlotIndexes.h" #include "llvm/CodeGen/TargetPassConfig.h" #include "gtest/gtest.h" -#include using namespace llvm; #if __has_feature(is_trivially_copyable) || (defined(__GNUC__) && __GNUC__ >= 5) -static_assert(std::is_trivially_copyable::value, "trivially copyable"); -static_assert(std::is_trivially_copyable::value, "trivially copyable"); -static_assert(std::is_trivially_copyable::value, "trivially copyable"); -static_assert(std::is_trivially_copyable::value, "trivially copyable"); -static_assert(std::is_trivially_copyable::value, "trivially copyable"); +static_assert(is_trivially_copyable::value, "trivially copyable"); +static_assert(is_trivially_copyable::value, "trivially copyable"); +static_assert(is_trivially_copyable::value, "trivially copyable"); +static_assert(is_trivially_copyable::value, "trivially copyable"); +static_assert(is_trivially_copyable::value, "trivially copyable"); #endif diff --git a/llvm/unittests/IR/CFGBuilder.cpp b/llvm/unittests/IR/CFGBuilder.cpp index c9bc52ca7a66..3583ab2a8a55 100644 --- a/llvm/unittests/IR/CFGBuilder.cpp +++ b/llvm/unittests/IR/CFGBuilder.cpp @@ -267,11 +267,10 @@ TEST(CFGBuilder, Rebuild) { EXPECT_TRUE(isa(B.getOrAddBlock("d")->getTerminator())); } -static_assert(std::is_trivially_copyable::value, +static_assert(is_trivially_copyable::value, "trivially copyable"); -static_assert(std::is_trivially_copyable::value, +static_assert(is_trivially_copyable::value, "trivially copyable"); -static_assert(std::is_trivially_copyable::value, - "trivially copyable"); -static_assert(std::is_trivially_copyable::value, +static_assert(is_trivially_copyable::value, "trivially copyable"); +static_assert(is_trivially_copyable::value, "trivially copyable"); diff --git a/llvm/unittests/Support/ScaledNumberTest.cpp b/llvm/unittests/Support/ScaledNumberTest.cpp index 82ecce09444d..3fa63b7bf3c7 100644 --- a/llvm/unittests/Support/ScaledNumberTest.cpp +++ b/llvm/unittests/Support/ScaledNumberTest.cpp @@ -562,7 +562,7 @@ TEST(ScaledNumberHelpersTest, toIntBug) { EXPECT_EQ(1u, (n * n).toInt()); } -static_assert(std::is_trivially_copyable>::value, +static_assert(is_trivially_copyable>::value, "trivially copyable"); } // end namespace diff --git a/llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn b/llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn index 193a59490560..389d5e962bc9 100644 --- a/llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn @@ -89,6 +89,7 @@ write_cmake_config("config") { "HAVE_LIBPSAPI=", "HAVE_MALLCTL=", "HAVE_SIGNAL_H=1", + "HAVE_STD_IS_TRIVIALLY_COPYABLE=1", "HAVE_STRERROR=1", "HAVE_SYS_STAT_H=1", "HAVE_SYS_TYPES_H=1",