public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jeffhurchalla at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/98801] New: Request for a conditional move built-in function
Date: Sat, 23 Jan 2021 04:47:06 +0000	[thread overview]
Message-ID: <bug-98801-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98801

            Bug ID: 98801
           Summary: Request for a conditional move built-in function
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeffhurchalla at gmail dot com
  Target Milestone: ---

There are a number of idiomatic ways to hint to gcc to generate a conditional
move, but there is no way that is documented/guaranteed.  In practice, they do
not always result in a conditional move.  I would like to request a builtin
function that generates a conditional move.

The current situation:
Perhaps the most common hint is to write
a = (cond) ? x : y
Another way is to bit-hack, and hope the compiler transforms it to cmov.  E.g.
a = ((-cond) & x) | ((cond-1) & y)
There are more ways still, see
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93165 .  None can be relied on for
the purpose.

Certainly a conditional move can be accomplished via inline asm, but this is
non-portable across ISAs, it's bug-prone, and it seems to hinder the optimizer.

The existing function __builtin_expect_with_probability is interesting: in
theory it seems like it might allow a user to get conditional moves via a
probability of 0.5.  I'm unsure if this works as desired in practice; its
documentation does not mention anything about conditional moves or
unpredictable branches.  (Also note that a probability of 0.5 does not
necessarily mean a condition is unpredictable - e.g. a condition could
alternate between true and false each time it's executed.)
Perhaps also interesting is clang's __builtin_unpredictable, which is
documented as a way to indicate to the compiler that a branch condition is
unpredictable.  I'm unsure here too if this affects conditional moves; their
docs don't mention it.

Personally, I'd like conditional moves for performance in situations where I
know a branch is completely unpredictable.  However, cmovs appear to also be
useful for security, to avoid timing attacks.

             reply	other threads:[~2021-01-23  4:47 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-23  4:47 jeffhurchalla at gmail dot com [this message]
2021-01-23  6:53 ` [Bug c++/98801] " pinskia at gcc dot gnu.org
2021-01-23 19:37 ` jeffhurchalla at gmail dot com
2021-01-23 20:28 ` jeffhurchalla at gmail dot com
2021-01-25  9:36 ` [Bug middle-end/98801] " rguenth at gcc dot gnu.org
2021-01-25 19:19 ` peter at cordes dot ca
2021-01-26  0:17 ` jeffhurchalla at gmail dot com
2021-01-27  5:18 ` jeffhurchalla at gmail dot com
2023-05-27 17:56 ` richard.yao at alumni dot stonybrook.edu

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-98801-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: link
Be 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).