This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Missed optimization with endian and alignment independent memory access on x64
- From: Moritz Strübe <moritz dot struebe at redheads dot de>
- To: GCC Development <gcc at gcc dot gnu dot org>
- Date: Thu, 6 Feb 2020 18:36:09 +0100
- Subject: Missed optimization with endian and alignment independent memory access on x64
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=redheads.de; dmarc=pass action=none header.from=redheads.de; dkim=pass header.d=redheads.de; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nNPoqnlHKE/jJ/81Dk2rF/YAijaW0L3o8926ZISZmcc=; b=Ie1WEX5d9HLitdxBng3+lSdW6jlP+5wtvMe7hilo0phtpyMFpn2hhZ9fFn3XtONqIA7B/qsLcFL+aCvwcbMPHuoc0YyNqHNwt8fqgtepbr1iMHCBcIBYY2O3vmtjytIZEOMO8chDV29hj5RFiyQx5A4N898jG32DHUR1SWVT1J9iQg2MDP4f9cla1tOUwGeOYHQIrgkMFhPkfUiYMWlvJjWcWxPfseJu9E/dtANAp5pCa/qYtI1f+6VhBzuozrC45cOxZ2Y4gD9jxnB3X+PzeWS3U+ZjuhklDJF0TvWh33ialyuu9x3R4867G/3sjGm/DKq/q5vWpF4ZJRdB07+wYg==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ji9FQQgOlDKb4NYvU6CtD7WA14YjoTJsvh5az/1TEb2qiXmyE8Jizw8bswCYhOJ46hIORih5nsgjuWppJqN1iTcPkd3mmJb9VIQLSENlnv0ViiStunTuWNPUGCwimNaBEAzr3f+rWStnRALPjvW0oEi8MzV/kD5tpcNgrTiKpuhY/K+3/JVcBWz0Dj5k9hAZ1UVJOT7Mv2i0mqZc0Qo5aSGYfNxG6UzIKER+Wvb/gpjxz3j6+2PMEZ8fM5dGxI3oMDfWmUxTLQdxzbzvlPye0HoPif4Y1n8EsWY5nfBiVmkW3/Vvm+dY1D8jQeNtpAud5Ztz+QLF7+V/yfUR4MCGdw==
Hey,
a pattern I see quite often in embedded libraries is to access an array
byte wise and shift the bits as needed (as this fixes endianness and
alignment issues). If I read two consecutive bytes and left-shift the
second by 8, I'd expect the compiler to optimize this to a word read on
a x64, as it is LE and supports unaligned reads.
Clang does this as expected, gcc however misses this.
Here are the examples: https://godbolt.org/z/qvCCNs
Thus my questions:
Why is this so hard optimize? As it's quite a common pattern I'd expect
that there would be at least some hand-coded special case optimizer.
(This isn't criticism - I'm honestly curious.) Or is there a reason gcc
shouldn't optimize this / Why it doesn't matter that this is missed?
Is there a way to write such code that gcc optimizes?
From a performance point of view: If I actually need two consecutive
bytes, wouldn't it be better to load them as word and split them at the
register level?
Cheers
Morty
--
Redheads Ltd. Softwaredienstleistungen
Schillerstr. 14
90409 Nürnberg
Telefon: +49 (0)911 180778-50
E-Mail: moritz.struebe@redheads.de | Web: www.redheads.de
Geschäftsführer: Andreas Hanke
Sitz der Gesellschaft: Lauf
Amtsgericht Nürnberg HRB 22681
Ust-ID: DE 249436843