diff --git a/include/llvm/Support/Compiler.h b/include/llvm/Support/Compiler.h index fce30e8b7de..c2ee882b754 100644 --- a/include/llvm/Support/Compiler.h +++ b/include/llvm/Support/Compiler.h @@ -151,7 +151,6 @@ #define LLVM_UNLIKELY(EXPR) (EXPR) #endif - // C++ doesn't support 'extern template' of template specializations. GCC does, // but requires __extension__ before it. In the header, use this: // EXTERN_TEMPLATE_INSTANTIATION(class foo); @@ -187,7 +186,6 @@ #define LLVM_ATTRIBUTE_ALWAYS_INLINE #endif - #ifdef __GNUC__ #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn)) #elif defined(_MSC_VER) @@ -225,6 +223,10 @@ #if defined(__clang__) || (__GNUC__ > 4) \ || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable() +#elif defined(_MSC_VER) +# define LLVM_BUILTIN_UNREACHABLE __assume(false) +#else +# define LLVM_BUILTIN_UNREACHABLE 0 #endif /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression @@ -236,4 +238,14 @@ # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0 #endif +/// \macro LLVM_ASSUME_ALIGNED +/// \brief Returns a pointer with an assumed alignment. +#if defined(__GNUC__) && !defined(__clang__) +// FIXME: Enable on clang when it supports it. +# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a) +#else +# define LLVM_ASSUME_ALIGNED(p, a) \ + (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p))) +#endif + #endif diff --git a/include/llvm/Support/Endian.h b/include/llvm/Support/Endian.h index 8d5649dc1f9..d438facfa4e 100644 --- a/include/llvm/Support/Endian.h +++ b/include/llvm/Support/Endian.h @@ -14,136 +14,78 @@ #ifndef LLVM_SUPPORT_ENDIAN_H #define LLVM_SUPPORT_ENDIAN_H +#include "llvm/Support/AlignOf.h" #include "llvm/Support/Host.h" #include "llvm/Support/SwapByteOrder.h" #include "llvm/Support/type_traits.h" namespace llvm { namespace support { +enum endianness {big, little, native}; -enum endianness {big, little}; -enum alignment {unaligned, aligned}; +// These are named values for common alignments. +enum {aligned = 0, unaligned = 1}; namespace detail { - -template -struct alignment_access_helper; - -template -struct alignment_access_helper -{ - value_type val; -}; - -// Provides unaligned loads and stores. -#pragma pack(push) -#pragma pack(1) -template -struct alignment_access_helper -{ - value_type val; -}; -#pragma pack(pop) - + /// \brief ::value is either alignment, or alignof(T) if alignment is 0. + template + struct PickAlignment { + enum {value = alignment == 0 ? AlignOf::Alignment : alignment}; + }; } // end namespace detail namespace endian { - template - inline value_type read_le(const void *memory) { - value_type t = - reinterpret_cast *>(memory)->val; - if (sys::isBigEndianHost()) - return sys::SwapByteOrder(t); - return t; - } - - template - inline void write_le(void *memory, value_type value) { - if (sys::isBigEndianHost()) - value = sys::SwapByteOrder(value); - reinterpret_cast *> - (memory)->val = value; - } - - template - inline value_type read_be(const void *memory) { - value_type t = - reinterpret_cast *>(memory)->val; - if (sys::isLittleEndianHost()) - return sys::SwapByteOrder(t); - return t; - } - - template - inline void write_be(void *memory, value_type value) { - if (sys::isLittleEndianHost()) - value = sys::SwapByteOrder(value); - reinterpret_cast *> - (memory)->val = value; - } +template +inline value_type byte_swap(value_type value) { + if (endian != native && sys::isBigEndianHost() != (endian == big)) + return sys::SwapByteOrder(value); + return value; } -namespace detail { - template -class packed_endian_specific_integral; + std::size_t alignment> +inline value_type read(const void *memory) { + value_type ret; -template -class packed_endian_specific_integral { -public: + memcpy(&ret, + LLVM_ASSUME_ALIGNED(memory, + (detail::PickAlignment::value)), + sizeof(value_type)); + return byte_swap(ret); +} + +template +inline void write(void *memory, value_type value) { + value = byte_swap(value); + memcpy(LLVM_ASSUME_ALIGNED(memory, + (detail::PickAlignment::value)), + &value, + sizeof(value_type)); +} +} // end namespace endian + +namespace detail { +template +struct packed_endian_specific_integral { operator value_type() const { - return endian::read_le(Value); + return endian::read( + (const void*)Value.buffer); } - void operator=(value_type newValue) { - endian::write_le((void *)&Value, newValue); - } -private: - uint8_t Value[sizeof(value_type)]; -}; -template -class packed_endian_specific_integral { -public: - operator value_type() const { - return endian::read_be(Value); - } void operator=(value_type newValue) { - endian::write_be((void *)&Value, newValue); + endian::write( + (void*)Value.buffer, newValue); } -private: - uint8_t Value[sizeof(value_type)]; -}; -template -class packed_endian_specific_integral { -public: - operator value_type() const { - return endian::read_le(&Value); - } - void operator=(value_type newValue) { - endian::write_le((void *)&Value, newValue); - } private: - value_type Value; + AlignedCharArray::value, + sizeof(value_type)> Value; }; - -template -class packed_endian_specific_integral { -public: - operator value_type() const { - return endian::read_be(&Value); - } - void operator=(value_type newValue) { - endian::write_be((void *)&Value, newValue); - } -private: - value_type Value; -}; - } // end namespace detail typedef detail::packed_endian_specific_integral @@ -218,6 +160,19 @@ typedef detail::packed_endian_specific_integral typedef detail::packed_endian_specific_integral aligned_big64_t; +typedef detail::packed_endian_specific_integral + unaligned_uint16_t; +typedef detail::packed_endian_specific_integral + unaligned_uint32_t; +typedef detail::packed_endian_specific_integral + unaligned_uint64_t; + +typedef detail::packed_endian_specific_integral + unaligned_int16_t; +typedef detail::packed_endian_specific_integral + unaligned_int32_t; +typedef detail::packed_endian_specific_integral + unaligned_int64_t; } // end namespace llvm } // end namespace support diff --git a/unittests/Support/EndianTest.cpp b/unittests/Support/EndianTest.cpp index 00ea2ae5d9a..8f935530638 100644 --- a/unittests/Support/EndianTest.cpp +++ b/unittests/Support/EndianTest.cpp @@ -21,36 +21,36 @@ namespace { TEST(Endian, Read) { // These are 5 bytes so we can be sure at least one of the reads is unaligned. - unsigned char big[] = {0x00, 0x01, 0x02, 0x03, 0x04}; - unsigned char little[] = {0x00, 0x04, 0x03, 0x02, 0x01}; + unsigned char bigval[] = {0x00, 0x01, 0x02, 0x03, 0x04}; + unsigned char littleval[] = {0x00, 0x04, 0x03, 0x02, 0x01}; int32_t BigAsHost = 0x00010203; - EXPECT_EQ(BigAsHost, (endian::read_be(big))); + EXPECT_EQ(BigAsHost, (endian::read(bigval))); int32_t LittleAsHost = 0x02030400; - EXPECT_EQ(LittleAsHost, (endian::read_le(little))); + EXPECT_EQ(LittleAsHost,(endian::read(littleval))); - EXPECT_EQ((endian::read_be(big + 1)), - (endian::read_le(little + 1))); + EXPECT_EQ((endian::read(bigval + 1)), + (endian::read(littleval + 1))); } TEST(Endian, Write) { unsigned char data[5]; - endian::write_be(data, -1362446643); + endian::write(data, -1362446643); EXPECT_EQ(data[0], 0xAE); EXPECT_EQ(data[1], 0xCA); EXPECT_EQ(data[2], 0xB6); EXPECT_EQ(data[3], 0xCD); - endian::write_be(data + 1, -1362446643); + endian::write(data + 1, -1362446643); EXPECT_EQ(data[1], 0xAE); EXPECT_EQ(data[2], 0xCA); EXPECT_EQ(data[3], 0xB6); EXPECT_EQ(data[4], 0xCD); - endian::write_le(data, -1362446643); + endian::write(data, -1362446643); EXPECT_EQ(data[0], 0xCD); EXPECT_EQ(data[1], 0xB6); EXPECT_EQ(data[2], 0xCA); EXPECT_EQ(data[3], 0xAE); - endian::write_le(data + 1, -1362446643); + endian::write(data + 1, -1362446643); EXPECT_EQ(data[1], 0xCD); EXPECT_EQ(data[2], 0xB6); EXPECT_EQ(data[3], 0xCA); @@ -69,4 +69,4 @@ TEST(Endian, PackedEndianSpecificIntegral) { EXPECT_EQ(*big_val, *little_val); } -} +} // end anon namespace diff --git a/utils/yaml2obj/yaml2obj.cpp b/utils/yaml2obj/yaml2obj.cpp index c9436090dd6..5a8ec1d0966 100644 --- a/utils/yaml2obj/yaml2obj.cpp +++ b/utils/yaml2obj/yaml2obj.cpp @@ -790,7 +790,8 @@ template raw_ostream &operator <<( raw_ostream &OS , const binary_le_impl &BLE) { char Buffer[sizeof(BLE.Value)]; - support::endian::write_le(Buffer, BLE.Value); + support::endian::write( + Buffer, BLE.Value); OS.write(Buffer, sizeof(BLE.Value)); return OS; }