From: Aurelien Jarno <aurelien@aurel32.net>
To: Richard Henderson <rth@twiddle.net>
Cc: Peter Maydell <peter.maydell@linaro.org>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] tcg-i386: Improve cmov detection
Date: Sun, 25 Nov 2012 14:44:18 +0100 [thread overview]
Message-ID: <20121125134418.GE4016@ohm.aurel32.net> (raw)
In-Reply-To: <1353778775-7477-1-git-send-email-rth@twiddle.net>
On Sat, Nov 24, 2012 at 09:39:35AM -0800, Richard Henderson wrote:
> In addition to better compile-time detection, perform runtime detection.
>
> Signed-off-by: Richard Henderson <rth@twiddle.net>
> ---
> tcg/i386/tcg-target.c | 34 +++++++++++++++++++++++++++++++++-
> tcg/i386/tcg-target.h | 5 -----
> 2 files changed, 33 insertions(+), 6 deletions(-)
>
> Yall are right that there's no particularly good method with which
> to detect i686 *or later*, and thus cmov support, in gcc. If one
> uses -march=native with any processor made in the last 5 years,
> one will have at least SSE1 support. So we can reasonably use that
> as a clue.
>
> To fill in the holes, we can do the check at runtime. That does
> involve a tiny amount of runtime overhead, testing a global variable.
> I suspect that this is overhead is unmeasurable.
If this overhead is unmesurable, and I think it is something true, I
think it would be better to just always use that on i386 (but not on
x86_64) instead of having a complex compile time detection that could
fail.
Otherwise the patch looks fine.
> diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
> index 6f3ad3c..b333b46 100644
> --- a/tcg/i386/tcg-target.c
> +++ b/tcg/i386/tcg-target.c
> @@ -97,6 +97,20 @@ static const int tcg_target_call_oarg_regs[] = {
> # define TCG_REG_L1 TCG_REG_EDX
> #endif
>
> +/* Attempt to determine at compile-time whether the compiler assumes that
> + cmov is available. We get 64-bit for free. P6 (i686) and later include
> + support for cmov, but there is no one preprocessor define that determines
> + this. Assume that all processors that include sse also support cmov, so
> + that we sorta future-proof this test against new preprocessor defines. */
> +#include <cpuid.h>
> +#if (TCG_TARGET_REG_BITS == 64 \
> + || defined(__i686__) || defined(__pentium4__) \
> + || defined(__athlon__) || defined(__SSE__))
> +# define have_cmov 1
> +#else
> +static bool have_cmov;
> +#endif
> +
> static uint8_t *tb_ret_addr;
>
> static void patch_reloc(uint8_t *code_ptr, int type,
> @@ -943,7 +957,14 @@ static void tcg_out_movcond32(TCGContext *s, TCGCond cond, TCGArg dest,
> TCGArg v1)
> {
> tcg_out_cmp(s, c1, c2, const_c2, 0);
> - tcg_out_modrm(s, OPC_CMOVCC | tcg_cond_to_jcc[cond], dest, v1);
> + if (have_cmov) {
> + tcg_out_modrm(s, OPC_CMOVCC | tcg_cond_to_jcc[cond], dest, v1);
> + } else {
> + int over = gen_new_label();
> + tcg_out_jxx(s, tcg_cond_to_jcc[tcg_invert_cond(cond)], over, 1);
> + tcg_out_mov(s, TCG_TYPE_I32, dest, v1);
> + tcg_out_label(s, over, s->code_ptr);
> + }
> }
>
> #if TCG_TARGET_REG_BITS == 64
> @@ -2243,6 +2264,17 @@ static void tcg_target_qemu_prologue(TCGContext *s)
>
> static void tcg_target_init(TCGContext *s)
> {
> + /* If we could not determine cmov availablity at compile time, perform
> + the check at runtime. 99% certainty that we're running on hardware
> + that supports cmov, but we still need to check. In case cmov is not
> + available, we'll use a small forward branch. */
> +#ifndef have_cmov
> + {
> + unsigned a, b, c, d;
> + have_cmov = (__get_cpuid(1, &a, &b, &c, &d) && (d & bit_CMOV));
> + }
> +#endif
> +
> #if !defined(CONFIG_USER_ONLY)
> /* fail safe */
> if ((1 << CPU_TLB_ENTRY_BITS) != sizeof(CPUTLBEntry))
> diff --git a/tcg/i386/tcg-target.h b/tcg/i386/tcg-target.h
> index dbc6756..450078b 100644
> --- a/tcg/i386/tcg-target.h
> +++ b/tcg/i386/tcg-target.h
> @@ -90,12 +90,7 @@ typedef enum {
> #define TCG_TARGET_HAS_nand_i32 0
> #define TCG_TARGET_HAS_nor_i32 0
> #define TCG_TARGET_HAS_deposit_i32 1
> -#if defined(__x86_64__) || defined(__i686__)
> -/* Use cmov only if the compiler is already doing so. */
> #define TCG_TARGET_HAS_movcond_i32 1
> -#else
> -#define TCG_TARGET_HAS_movcond_i32 0
> -#endif
>
> #if TCG_TARGET_REG_BITS == 64
> #define TCG_TARGET_HAS_div2_i64 1
> --
> 1.7.11.7
>
>
--
Aurelien Jarno GPG: 1024D/F1BCDB73
aurelien@aurel32.net http://www.aurel32.net
next prev parent reply other threads:[~2012-11-25 13:44 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-11-23 15:12 [Qemu-trivial] [PATCH] qemu-timer: Don't use RDTSC on 386s and 486s Peter Maydell
2012-11-23 15:12 ` [Qemu-devel] " Peter Maydell
2012-11-23 15:15 ` [Qemu-trivial] " Paolo Bonzini
2012-11-23 15:15 ` [Qemu-devel] " Paolo Bonzini
2012-11-23 15:17 ` [Qemu-trivial] " Peter Maydell
2012-11-23 15:17 ` [Qemu-devel] " Peter Maydell
2012-11-23 15:31 ` [Qemu-trivial] " Jamie Lokier
2012-11-23 15:31 ` Jamie Lokier
2012-11-23 15:38 ` [Qemu-trivial] " Peter Maydell
2012-11-23 15:38 ` Peter Maydell
2012-11-23 16:21 ` [Qemu-trivial] " Jamie Lokier
2012-11-23 16:21 ` Jamie Lokier
2012-11-23 15:37 ` [Qemu-trivial] " Peter Maydell
2012-11-23 15:37 ` [Qemu-devel] " Peter Maydell
2012-11-23 16:19 ` [Qemu-trivial] " Jamie Lokier
2012-11-23 16:19 ` Jamie Lokier
2012-11-24 17:39 ` [Qemu-devel] [PATCH] tcg-i386: Improve cmov detection Richard Henderson
2012-11-24 18:12 ` Peter Maydell
2012-11-26 16:23 ` Richard Henderson
2012-12-10 15:42 ` 陳韋任 (Wei-Ren Chen)
2012-11-25 13:44 ` Aurelien Jarno [this message]
2012-12-03 13:00 ` [Qemu-trivial] [PATCH] qemu-timer: Don't use RDTSC on 386s and 486s Stefan Hajnoczi
2012-12-03 13:00 ` [Qemu-devel] " Stefan Hajnoczi
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=20121125134418.GE4016@ohm.aurel32.net \
--to=aurelien@aurel32.net \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=rth@twiddle.net \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.