diff mbox series

[v6,18/50] tcg: Reserve temporary index 0

Message ID 20171016172609.23422-19-richard.henderson@linaro.org
State New
Headers show
Series tcg tb_lock removal | expand

Commit Message

Richard Henderson Oct. 16, 2017, 5:25 p.m. UTC
Since we cast indicies to pointers, reserving 0 allows
us to use NULL for unused/dummy instead of (T *)-1.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

---
 tcg/tcg.h | 16 ++++++++--------
 tcg/tcg.c |  5 ++++-
 2 files changed, 12 insertions(+), 9 deletions(-)

-- 
2.13.6

Comments

Emilio Cota Oct. 17, 2017, 11:19 p.m. UTC | #1
On Mon, Oct 16, 2017 at 10:25:37 -0700, Richard Henderson wrote:
> Since we cast indicies to pointers, reserving 0 allows


s/indicies/indices/

> us to use NULL for unused/dummy instead of (T *)-1.

> 

> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

> ---

(snip)
> @@ -737,7 +737,7 @@ extern bool parallel_cpus;

>  static inline size_t temp_idx(TCGTemp *ts)

>  {

>      ptrdiff_t n = ts - tcg_ctx.temps;

> -    tcg_debug_assert(n >= 0 && n < tcg_ctx.nb_temps);

> +    tcg_debug_assert(n > 0 && n < tcg_ctx.nb_temps);

>      return n;

>  }

>  

> diff --git a/tcg/tcg.c b/tcg/tcg.c

> index 129aecca60..7cf39f7067 100644

> --- a/tcg/tcg.c

> +++ b/tcg/tcg.c

> @@ -333,7 +333,10 @@ void tcg_context_init(TCGContext *s)

>      int *sorted_args;

>  

>      memset(s, 0, sizeof(*s));

> -    s->nb_globals = 0;

> +    /* Reserve temp index 0 so that, with the funny casting that we do,

> +       the first one doesn't look like NULL.  */

> +    s->nb_globals = 1;

> +    s->nb_temps = 1;

>  

>      /* Count total number of arguments and allocate the corresponding

>         space */


I like this change, although operating on the 0th element makes
me uneasy.

For instance, I managed to trigger the above assert by manually calling dump_regs
(it is otherwise called before aborting, which is why you missed it.)
This fixes it:

diff --git a/tcg/tcg.c b/tcg/tcg.c
index 7cf39f7..49176e0 100644
--- a/tcg/tcg.c
+++ b/tcg/tcg.c
@@ -2034,7 +2034,7 @@ static void dump_regs(TCGContext *s)
     int i;
     char buf[64];
 
-    for(i = 0; i < s->nb_temps; i++) {
+    for (i = 1; i < s->nb_temps; i++) {
         ts = &s->temps[i];
         printf("  %10s: ", tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
         switch(ts->val_type) {

Is it worth changing other [0, nb_temps/globals) loops to start from 1?

Thanks,

		E.
diff mbox series

Patch

diff --git a/tcg/tcg.h b/tcg/tcg.h
index b8ede7fe5c..ccf1bcdaf6 100644
--- a/tcg/tcg.h
+++ b/tcg/tcg.h
@@ -471,13 +471,13 @@  static inline intptr_t QEMU_ARTIFICIAL GET_TCGV_PTR(TCGv_ptr t)
 #define TCGV_EQUAL_PTR(a, b) (GET_TCGV_PTR(a) == GET_TCGV_PTR(b))
 
 /* Dummy definition to avoid compiler warnings.  */
-#define TCGV_UNUSED_I32(x) x = MAKE_TCGV_I32(-1)
-#define TCGV_UNUSED_I64(x) x = MAKE_TCGV_I64(-1)
-#define TCGV_UNUSED_PTR(x) x = MAKE_TCGV_PTR(-1)
+#define TCGV_UNUSED_I32(x) ((x) = NULL)
+#define TCGV_UNUSED_I64(x) ((x) = NULL)
+#define TCGV_UNUSED_PTR(x) ((x) = NULL)
 
-#define TCGV_IS_UNUSED_I32(x) (GET_TCGV_I32(x) == -1)
-#define TCGV_IS_UNUSED_I64(x) (GET_TCGV_I64(x) == -1)
-#define TCGV_IS_UNUSED_PTR(x) (GET_TCGV_PTR(x) == -1)
+#define TCGV_IS_UNUSED_I32(x) (GET_TCGV_I32(x) == 0)
+#define TCGV_IS_UNUSED_I64(x) (GET_TCGV_I64(x) == 0)
+#define TCGV_IS_UNUSED_PTR(x) (GET_TCGV_PTR(x) == 0)
 
 /* call flags */
 /* Helper does not read globals (either directly or through an exception). It
@@ -496,7 +496,7 @@  static inline intptr_t QEMU_ARTIFICIAL GET_TCGV_PTR(TCGv_ptr t)
 #define TCG_CALL_NO_WG_SE       (TCG_CALL_NO_WG | TCG_CALL_NO_SE)
 
 /* used to align parameters */
-#define TCG_CALL_DUMMY_ARG      ((TCGArg)(-1))
+#define TCG_CALL_DUMMY_ARG      ((TCGArg)0)
 
 /* Conditions.  Note that these are laid out for easy manipulation by
    the functions below:
@@ -737,7 +737,7 @@  extern bool parallel_cpus;
 static inline size_t temp_idx(TCGTemp *ts)
 {
     ptrdiff_t n = ts - tcg_ctx.temps;
-    tcg_debug_assert(n >= 0 && n < tcg_ctx.nb_temps);
+    tcg_debug_assert(n > 0 && n < tcg_ctx.nb_temps);
     return n;
 }
 
diff --git a/tcg/tcg.c b/tcg/tcg.c
index 129aecca60..7cf39f7067 100644
--- a/tcg/tcg.c
+++ b/tcg/tcg.c
@@ -333,7 +333,10 @@  void tcg_context_init(TCGContext *s)
     int *sorted_args;
 
     memset(s, 0, sizeof(*s));
-    s->nb_globals = 0;
+    /* Reserve temp index 0 so that, with the funny casting that we do,
+       the first one doesn't look like NULL.  */
+    s->nb_globals = 1;
+    s->nb_temps = 1;
 
     /* Count total number of arguments and allocate the corresponding
        space */