diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h index 36539f6d346d..6ca86bdd5026 100644 --- a/llvm/include/llvm/Support/Compiler.h +++ b/llvm/include/llvm/Support/Compiler.h @@ -311,4 +311,17 @@ # define LLVM_MEMORY_SANITIZER_BUILD 0 #endif +/// \macro LLVM_IS_UNALIGNED_ACCESS_FAST +/// \brief Is unaligned memory access fast on the host machine. +/// +/// Don't specialize on alignment for platforms where unaligned memory accesses +/// generates the same code as aligned memory accesses for common types. +#if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \ + defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \ + defined(_X86_) || defined(__i386) || defined(__i386__) +# define LLVM_IS_UNALIGNED_ACCESS_FAST 1 +#else +# define LLVM_IS_UNALIGNED_ACCESS_FAST 0 +#endif + #endif diff --git a/llvm/lib/Object/ELFObjectFile.cpp b/llvm/lib/Object/ELFObjectFile.cpp index 160053dabd87..cfe0eb467e53 100644 --- a/llvm/lib/Object/ELFObjectFile.cpp +++ b/llvm/lib/Object/ELFObjectFile.cpp @@ -27,30 +27,42 @@ ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { 1ULL << CountTrailingZeros_64(uintptr_t(Object->getBufferStart())); if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) +#if !LLVM_IS_UNALIGNED_ACCESS_FAST if (MaxAlignment >= 4) return new ELFObjectFile >(Object, ec); - else if (MaxAlignment >= 2) + else +#endif + if (MaxAlignment >= 2) return new ELFObjectFile >(Object, ec); else llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) +#if !LLVM_IS_UNALIGNED_ACCESS_FAST if (MaxAlignment >= 4) return new ELFObjectFile >(Object, ec); - else if (MaxAlignment >= 2) + else +#endif + if (MaxAlignment >= 2) return new ELFObjectFile >(Object, ec); else llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) +#if !LLVM_IS_UNALIGNED_ACCESS_FAST if (MaxAlignment >= 8) return new ELFObjectFile >(Object, ec); - else if (MaxAlignment >= 2) + else +#endif + if (MaxAlignment >= 2) return new ELFObjectFile >(Object, ec); else llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { +#if !LLVM_IS_UNALIGNED_ACCESS_FAST if (MaxAlignment >= 8) return new ELFObjectFile >(Object, ec); - else if (MaxAlignment >= 2) + else +#endif + if (MaxAlignment >= 2) return new ELFObjectFile >(Object, ec); else llvm_unreachable("Invalid alignment for ELF file!");