public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "thiago at kde dot org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug target/109896] New: Missed optimisation: overflow detection in multiplication instructions for operator new Date: Wed, 17 May 2023 18:34:39 +0000 [thread overview] Message-ID: <bug-109896-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109896 Bug ID: 109896 Summary: Missed optimisation: overflow detection in multiplication instructions for operator new Product: gcc Version: 13.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: thiago at kde dot org Target Milestone: --- In the following code: struct S { char buf[47]; // weird size }; void *f(unsigned long paramCount) { return new S[paramCount]; } GCC generates (see https://gcc.godbolt.org/z/o5eocj5n9): movabsq $196241958230952676, %rax cmpq %rdi, %rax jb .L2 imulq $47, %rdi, %rdi jmp operator new[](unsigned long) f(unsigned long) [clone .cold]: .L2: pushq %rax call __cxa_throw_bad_array_new_length That's a slight pessimisation of the typical, non-exceptional case because of the presence of the compare instructions. On modern x86, that's 3 retire slots and 2 uops, in addition to the multiplication's 3 cycles (which may be speculated and start early). But the presence of a 10-byte instruction and the fact that the jump is further than 8-bit displacement range mean those three instructions occupy 18 bytes, meaning the front-end is sub-utilised, requiring 2 cycles to decode the 5 instructions (pre-GLC [I think] CPUs decode 4 instructions in 16 bytes per cycle). Instead, GCC should emit the multiplication and check if the overflow flag was set. I believe the optimal code for GCC would be: imulq $47, %rdi, %rdi jo .L2 jmp operator new[](unsigned long) That's 15 bytes, so 1 cycle for the decoder to decode all 3 instructions. That's 3+1 cycles and 2 retire slots before the JMP. In the Godbolt link above, Clang and MSVC emitted a CMOV: mulq %rcx movq $-1, %rdi cmovnoq %rax, %rdi jmp operator new[](unsigned long)@PLT This is slightly worse (19 bytes, 4 instructions, though also 3+1 cycles). For GCC's -fno-exceptions case, I recommend keeping the IMUL+JO case and only load -1 in the .text.unlikely section. But see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109895
next reply other threads:[~2023-05-17 18:34 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-05-17 18:34 thiago at kde dot org [this message] 2023-05-17 18:45 ` [Bug target/109896] " pinskia at gcc dot gnu.org 2023-05-17 20:39 ` hjl.tools at gmail dot com 2023-05-17 23:42 ` thiago at kde dot org 2023-05-17 23:46 ` pinskia at gcc dot gnu.org 2023-05-18 0:13 ` thiago at kde dot org 2023-05-18 8:33 ` redi at gcc dot gnu.org 2023-05-18 14:08 ` thiago at kde dot org 2024-06-06 16:49 ` [Bug c++/109896] " pinskia at gcc dot gnu.org 2024-06-06 22:03 ` pinskia at gcc dot gnu.org 2024-06-07 1:42 ` pinskia at gcc dot gnu.org 2024-06-07 14:05 ` pinskia at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-109896-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).