diff mbox series

[v5,03/36] tcg: Allocate objects contiguously in temp_allocate_frame

Message ID 20230126043824.54819-4-richard.henderson@linaro.org
State Superseded
Headers show
Series tcg: Support for Int128 with helpers | expand

Commit Message

Richard Henderson Jan. 26, 2023, 4:37 a.m. UTC
When allocating a temp to the stack frame, consider the
base type and allocate all parts at once.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 tcg/tcg.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

Comments

Alex Bennée Jan. 26, 2023, 5:12 p.m. UTC | #1
Richard Henderson <richard.henderson@linaro.org> writes:

> When allocating a temp to the stack frame, consider the
> base type and allocate all parts at once.
>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  tcg/tcg.c | 30 ++++++++++++++++++++++--------
>  1 file changed, 22 insertions(+), 8 deletions(-)
>
> diff --git a/tcg/tcg.c b/tcg/tcg.c
> index ffddda96ed..ff30f5e141 100644
> --- a/tcg/tcg.c
> +++ b/tcg/tcg.c
> @@ -3264,11 +3264,12 @@ static bool liveness_pass_2(TCGContext *s)
>  
>  static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
>  {
> -    int size = tcg_type_size(ts->type);
> -    int align;
>      intptr_t off;
> +    int size, align;
>  
> -    switch (ts->type) {
> +    /* When allocating an object, look at the full type. */
> +    size = tcg_type_size(ts->base_type);
> +    switch (ts->base_type) {
>      case TCG_TYPE_I32:
>          align = 4;
>          break;
> @@ -3299,13 +3300,26 @@ static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
>          tcg_raise_tb_overflow(s);
>      }
>      s->current_frame_offset = off + size;
> -
> -    ts->mem_offset = off;
>  #if defined(__sparc__)
> -    ts->mem_offset += TCG_TARGET_STACK_BIAS;
> +    off += TCG_TARGET_STACK_BIAS;
>  #endif
> -    ts->mem_base = s->frame_temp;
> -    ts->mem_allocated = 1;
> +
> +    /* If the object was subdivided, assign memory to all the parts. */
> +    if (ts->base_type != ts->type) {
> +        int part_size = tcg_type_size(ts->type);
> +        int part_count = size / part_size;
> +
> +        ts -= ts->temp_subindex;

This seems a bit magic to me. Are we saying we always know there are
TCGTemps "behind" ts because that is implied by temp_subindex?

I guess:

        for (i = 1; i < n; ++i) {
                TCGTemp *ts2 = tcg_temp_alloc(s);

in tcg_temp_new_internal() implies this?

Maybe a comment like "tcg_temp_new_internal() ensures all TCGTemps with
subindexes will be sequential to the first part in memory"?

> +        for (int i = 0; i < part_count; ++i) {
> +            ts[i].mem_offset = off + i * part_size;
> +            ts[i].mem_base = s->frame_temp;
> +            ts[i].mem_allocated = 1;
> +        }
> +    } else {
> +        ts->mem_offset = off;
> +        ts->mem_base = s->frame_temp;
> +        ts->mem_allocated = 1;
> +    }
>  }

It might be worth doing some documentation of the various parts of
TCGTemp to help follow this.

Otherwise:

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Richard Henderson Jan. 26, 2023, 7:48 p.m. UTC | #2
On 1/26/23 07:12, Alex Bennée wrote:
>> +        ts -= ts->temp_subindex;
> 
> This seems a bit magic to me. Are we saying we always know there are
> TCGTemps "behind" ts because that is implied by temp_subindex?

Not "implied" so much as "defined".

> It might be worth doing some documentation of the various parts of
> TCGTemp to help follow this.

Ok.


r~
diff mbox series

Patch

diff --git a/tcg/tcg.c b/tcg/tcg.c
index ffddda96ed..ff30f5e141 100644
--- a/tcg/tcg.c
+++ b/tcg/tcg.c
@@ -3264,11 +3264,12 @@  static bool liveness_pass_2(TCGContext *s)
 
 static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
 {
-    int size = tcg_type_size(ts->type);
-    int align;
     intptr_t off;
+    int size, align;
 
-    switch (ts->type) {
+    /* When allocating an object, look at the full type. */
+    size = tcg_type_size(ts->base_type);
+    switch (ts->base_type) {
     case TCG_TYPE_I32:
         align = 4;
         break;
@@ -3299,13 +3300,26 @@  static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
         tcg_raise_tb_overflow(s);
     }
     s->current_frame_offset = off + size;
-
-    ts->mem_offset = off;
 #if defined(__sparc__)
-    ts->mem_offset += TCG_TARGET_STACK_BIAS;
+    off += TCG_TARGET_STACK_BIAS;
 #endif
-    ts->mem_base = s->frame_temp;
-    ts->mem_allocated = 1;
+
+    /* If the object was subdivided, assign memory to all the parts. */
+    if (ts->base_type != ts->type) {
+        int part_size = tcg_type_size(ts->type);
+        int part_count = size / part_size;
+
+        ts -= ts->temp_subindex;
+        for (int i = 0; i < part_count; ++i) {
+            ts[i].mem_offset = off + i * part_size;
+            ts[i].mem_base = s->frame_temp;
+            ts[i].mem_allocated = 1;
+        }
+    } else {
+        ts->mem_offset = off;
+        ts->mem_base = s->frame_temp;
+        ts->mem_allocated = 1;
+    }
 }
 
 /* Assign @reg to @ts, and update reg_to_temp[]. */