Patchwork tcg-i386: Perform cmov detection at runtime for 32-bit.

login
register
mail settings
Submitter Richard Henderson
Date Dec. 28, 2012, 10:17 p.m.
Message ID <1356733022-4341-1-git-send-email-rth@twiddle.net>
Download mbox | patch
Permalink /patch/208602/
State New
Headers show

Comments

Richard Henderson - Dec. 28, 2012, 10:17 p.m.
Existing compile-time detection is spotty at best.  Convert
it all to runtime detection instead.

Signed-off-by: Richard Henderson <rth@twiddle.net>
---
 configure             | 19 +++++++++++++++++++
 tcg/i386/tcg-target.c | 31 ++++++++++++++++++++++++++++++-
 tcg/i386/tcg-target.h |  5 -----
 3 files changed, 49 insertions(+), 6 deletions(-)
Blue Swirl - Dec. 29, 2012, 3:51 p.m.
Thanks, applied.

On Fri, Dec 28, 2012 at 10:17 PM, Richard Henderson <rth@twiddle.net> wrote:
> Existing compile-time detection is spotty at best.  Convert
> it all to runtime detection instead.
>
> Signed-off-by: Richard Henderson <rth@twiddle.net>
> ---
>  configure             | 19 +++++++++++++++++++
>  tcg/i386/tcg-target.c | 31 ++++++++++++++++++++++++++++++-
>  tcg/i386/tcg-target.h |  5 -----
>  3 files changed, 49 insertions(+), 6 deletions(-)
>
> diff --git a/configure b/configure
> index 99c1ec3..b0c7e54 100755
> --- a/configure
> +++ b/configure
> @@ -3086,6 +3086,21 @@ if compile_prog "" "" ; then
>      has_environ=yes
>  fi
>
> +########################################
> +# check if cpuid.h is usable.
> +
> +cpuid_h=no
> +cat > $TMPC << EOF
> +#include <cpuid.h>
> +int main(void) {
> +  return 0;
> +}
> +EOF
> +if compile_prog "" "" ; then
> +    cpuid_h=yes
> +fi
> +
> +
>  ##########################################
>  # End of CC checks
>  # After here, no more $cc or $ld runs
> @@ -3611,6 +3626,10 @@ if test "$has_environ" = "yes" ; then
>    echo "CONFIG_HAS_ENVIRON=y" >> $config_host_mak
>  fi
>
> +if test "$cpuid_h" = "yes" ; then
> +  echo "CONFIG_CPUID_H=y" >> $config_host_mak
> +fi
> +
>  if test "$glusterfs" = "yes" ; then
>    echo "CONFIG_GLUSTERFS=y" >> $config_host_mak
>  fi
> diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
> index ae82746..e083874 100644
> --- a/tcg/i386/tcg-target.c
> +++ b/tcg/i386/tcg-target.c
> @@ -97,6 +97,18 @@ static const int tcg_target_call_oarg_regs[] = {
>  # define TCG_REG_L1 TCG_REG_EDX
>  #endif
>
> +/* For 32-bit, we are going to attempt to determine at runtime whether cmov
> +   is available.  However, the host compiler must supply <cpuid.h>, as we're
> +   not going to go so far as our own inline assembly.  */
> +#if TCG_TARGET_REG_BITS == 64
> +# define have_cmov 1
> +#elif defined(CONFIG_CPUID_H)
> +#include <cpuid.h>
> +static bool have_cmov;
> +#else
> +# define have_cmov 0
> +#endif
> +
>  static uint8_t *tb_ret_addr;
>
>  static void patch_reloc(uint8_t *code_ptr, int type,
> @@ -943,7 +955,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 +2262,16 @@ static void tcg_target_qemu_prologue(TCGContext *s)
>
>  static void tcg_target_init(TCGContext *s)
>  {
> +    /* For 32-bit, 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 5352ac0..e63db9c 100644
> --- a/tcg/i386/tcg-target.h
> +++ b/tcg/i386/tcg-target.h
> @@ -91,12 +91,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
>

Patch

diff --git a/configure b/configure
index 99c1ec3..b0c7e54 100755
--- a/configure
+++ b/configure
@@ -3086,6 +3086,21 @@  if compile_prog "" "" ; then
     has_environ=yes
 fi
 
+########################################
+# check if cpuid.h is usable.
+
+cpuid_h=no
+cat > $TMPC << EOF
+#include <cpuid.h>
+int main(void) {
+  return 0;
+}
+EOF
+if compile_prog "" "" ; then
+    cpuid_h=yes
+fi
+
+
 ##########################################
 # End of CC checks
 # After here, no more $cc or $ld runs
@@ -3611,6 +3626,10 @@  if test "$has_environ" = "yes" ; then
   echo "CONFIG_HAS_ENVIRON=y" >> $config_host_mak
 fi
 
+if test "$cpuid_h" = "yes" ; then
+  echo "CONFIG_CPUID_H=y" >> $config_host_mak
+fi
+
 if test "$glusterfs" = "yes" ; then
   echo "CONFIG_GLUSTERFS=y" >> $config_host_mak
 fi
diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
index ae82746..e083874 100644
--- a/tcg/i386/tcg-target.c
+++ b/tcg/i386/tcg-target.c
@@ -97,6 +97,18 @@  static const int tcg_target_call_oarg_regs[] = {
 # define TCG_REG_L1 TCG_REG_EDX
 #endif
 
+/* For 32-bit, we are going to attempt to determine at runtime whether cmov
+   is available.  However, the host compiler must supply <cpuid.h>, as we're
+   not going to go so far as our own inline assembly.  */
+#if TCG_TARGET_REG_BITS == 64
+# define have_cmov 1
+#elif defined(CONFIG_CPUID_H)
+#include <cpuid.h>
+static bool have_cmov;
+#else
+# define have_cmov 0
+#endif
+
 static uint8_t *tb_ret_addr;
 
 static void patch_reloc(uint8_t *code_ptr, int type,
@@ -943,7 +955,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 +2262,16 @@  static void tcg_target_qemu_prologue(TCGContext *s)
 
 static void tcg_target_init(TCGContext *s)
 {
+    /* For 32-bit, 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 5352ac0..e63db9c 100644
--- a/tcg/i386/tcg-target.h
+++ b/tcg/i386/tcg-target.h
@@ -91,12 +91,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