🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++
Article #975

Re: will it be optimized?

#975
From: Andrey Tarasevich
Date: Sat, 21 Nov 2026 15:54
94 lines
3699 bytes
On Wed 10/7/2026 12:01 AM, Andrey Tarasevich wrote:
> I was quite surprised to discover that GCC treats returning struct types 
> by value overly literally, i.e. fails to optimize it at all. For reasons 
> that defy any rationalization, GCC 16 translates this
Another example I recently discovered, which also looks quite perplexing 
is this

   typedef struct P
   {
     unsigned long long r;
     unsigned char e;
   } P;

   P foo(void)
   {
     unsigned long long r = rand();
     return (P) { r, r % 2 };
   }

which in GCC 16 (-O3) generates the following

   "foo":
         sub     rsp, 24
         call    "rand"
         mov     DWORD PTR [rsp], 0
         movsxd  rsi, eax
         mov     DWORD PTR [rsp+3], 0
         movabs  rax, 72057594037927935
         and     rax, QWORD PTR [rsp]
         sal     rax, 8
         add     rsp, 24
         mov     rdx, rax
         mov     rax, rsi
         mov     dl, sil
         and     dl, 1
         ret

https://godbolt.org/z/s6r5n6bj9

The struct type is small, so the result is returned in RAX:RDX register 
pair, which is great. However, as you can see, the code still fiddles 
with local memory for some reason... All these weird manipulations (two 
movs, movabs, and, sal) are there simply to lay out a sequence of 7 (!?) 
zero bytes in memory (!?), then load them into the register as a way to 
zero-initialize the padding bytes (!) in that register. They spend all 
this circuitous effort just to zero-initialize padding, Karl!

Why?!

It appears that the use of a compound literal is what triggers the 
strange code generation, since in the following versions of the function

   P bar(void)
   {
     P p = { 0 };
     p.r = rand();
     p.e = p.r % 2;
     return p;
   }

   P baz(void)
   {
     unsigned long long r = rand();
     P p = { r, r % 2 };
     return p;
   }

the resultant x86 code looks completely unremarkable

   "bar":
         sub     rsp, 8
         call    "rand"
         add     rsp, 8
         mov     ecx, eax
         cdqe
         movzx   edx, cl
         and     dl, 1
         ret

The C++ version of the original function (i.e. simply using {}-list 
under `return` instead of a compound literal) suffers from the same 
problem in C++ code

   P foo(void)
   {
     unsigned long long r = rand();
     return { r, r % 2 };
   }

https://godbolt.org/z/xfzKbqnc4

-- 
Best regards,
Andrey
Technical Details
Message-ID: <11dsequ$6r1q$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11a1f4c$1g3sg$1@dont-email.me> <11a4qor$2kj95$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Andrey Tarasevich <noone@noone.net>
Newsgroups: comp.lang.c,comp.lang.c++
Subject: Re: will it be optimized?
Date: Sat, 21 Nov 2026 07:54:04 -0800
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <11dsequ$6r1q$1@dont-email.me>
References: <11a1f4c$1g3sg$1@dont-email.me> <11a4qor$2kj95$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 21 Nov 2026 15:54:07 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="224314"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+IDlIzCCcv71pxelg1JDl8"; posting-host="9131a567af89f956165d9b91e3ccf192"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:gWL/CM/4kzXx2WZ61zR22v50OJA= sha256:NtzX/kSzLL6V7uY2CEjgBh/q01wTwB5CTlWSNzWIpUI=
	sha1:tdBBSRHkjuzEsWissJhX2BFi04Y= sha256:U9Ntrgy5eMLWa/jk3EPrsaMOMaRDmlnXWsdvffC/t2E=
Content-Language: en-US
In-Reply-To: <11a4qor$2kj95$1@dont-email.me>