Skip to contentSkip to conversation

target/hppa: Speed up hppa_is_pa20()

PATCH Superseded Helge Deller 4 participants 5 messages Dec 28, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/hppa: Speed up hppa_is_pa20()

Although the hppa_is_pa20() helper is costly due to string comparisms in object_dynamic_cast(), it is called quite often during memory lookups and at each start of a block of instruction translations. Speed hppa_is_pa20() up by calling object_dynamic_cast() only once at CPU creation and store the result in the is_pa20 of struct CPUArchState.

hw/hppa/machine.c +1 -0
index a31dc32a9f..08ac1ec068 100644
@@ -281,6 +281,7 @@ static TranslateFn *machine_HP_common_init_cpus(MachineState *machine)
     /* Create CPUs.  */
     for (unsigned int i = 0; i < smp_cpus; i++) {
         cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
+        cpu[i]->env.is_pa20 = object_dynamic_cast(OBJECT(cpu[i]), TYPE_HPPA64_CPU);
     }
 
     /*
target/hppa/cpu.h +2 -1
index e45ba50a59..c37a701f44 100644
@@ -208,6 +208,7 @@ typedef struct CPUArchState {
     uint64_t fr[32];
     uint64_t sr[8];          /* stored shifted into place for gva */
 
+    bool is_pa20;
     uint32_t psw;            /* All psw bits except the following:  */
     uint32_t psw_xb;         /* X and B, in their normal positions */
     target_ulong psw_n;      /* boolean */
@@ -294,7 +295,7 @@ struct HPPACPUClass {
 
 static inline bool hppa_is_pa20(CPUHPPAState *env)
 {
-    return object_dynamic_cast(OBJECT(env_cpu(env)), TYPE_HPPA64_CPU) != NULL;
+    return env->is_pa20;
 }
 
 static inline int HPPA_BTLB_ENTRIES(CPUHPPAState *env)
#1 lore raw
Quoted 33 lines · Helge Deller

Although the hppa_is_pa20() helper is costly due to string comparisms in object_dynamic_cast(), it is called quite often during memory lookups and at each start of a block of instruction translations. Speed hppa_is_pa20() up by calling object_dynamic_cast() only once at CPU creation and store the result in the is_pa20 of struct CPUArchState.

hw/hppa/machine.c +1 -0
index a31dc32a9f..08ac1ec068 100644
@@ -281,6 +281,7 @@ static TranslateFn *machine_HP_common_init_cpus(MachineState *machine)
    /* Create CPUs.  */
    for (unsigned int i = 0; i < smp_cpus; i++) {
        cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
+        cpu[i]->env.is_pa20 = object_dynamic_cast(OBJECT(cpu[i]), TYPE_HPPA64_CPU);
    }
 
    /*
target/hppa/cpu.h +1 -0
index e45ba50a59..c37a701f44 100644
@@ -208,6 +208,7 @@ typedef struct CPUArchState {
    uint64_t fr[32];
    uint64_t sr[8];          /* stored shifted into place for gva */
 
+    bool is_pa20;
    uint32_t psw;            /* All psw bits except the following:  */
    uint32_t psw_xb;         /* X and B, in their normal positions */
    target_ulong psw_n;      /* boolean */

target/hppa/cpu.h struct HPPACPUClass { · line 299

Quoted 6 lines
target/hppa/cpu.h +1 -1
@@ -294,7 +295,7 @@ struct HPPACPUClass {
 
 static inline bool hppa_is_pa20(CPUHPPAState *env)
 {
-    return object_dynamic_cast(OBJECT(env_cpu(env)), TYPE_HPPA64_CPU) != NULL;
+    return env->is_pa20;
 }

Now this function name is longer than what it extends to so maybe it would be simpler to drop the inline function and use env->is_pa20 directly where it's needed? Is there a reason to keep the function?

Regards,
BALATON Zoltan

Quoted 4 lines
target/hppa/cpu.h +0 -0
 static inline int HPPA_BTLB_ENTRIES(CPUHPPAState *env)
 
#2 lore raw
Quoted 35 lines · BALATON Zoltan
Quoted 34 lines · Helge Deller

Although the hppa_is_pa20() helper is costly due to string comparisms in object_dynamic_cast(), it is called quite often during memory lookups and at each start of a block of instruction translations. Speed hppa_is_pa20() up by calling object_dynamic_cast() only once at CPU creation and store the result in the is_pa20 of struct CPUArchState.

hw/hppa/machine.c +1 -0
index a31dc32a9f..08ac1ec068 100644
@@ -281,6 +281,7 @@ static TranslateFn *machine_HP_common_init_cpus(MachineState *machine)
     /* Create CPUs.  */
     for (unsigned int i = 0; i < smp_cpus; i++) {
         cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
+        cpu[i]->env.is_pa20 = object_dynamic_cast(OBJECT(cpu[i]), TYPE_HPPA64_CPU);
     }
 
     /*
target/hppa/cpu.h +0 -0
index e45ba50a59..c37a701f44 100644
@@ -208,6 +208,7 @@ typedef struct CPUArchState {
     uint64_t fr[32];

    uint64_t sr[8];          /* stored shifted into place for gva */

+    bool is_pa20;
    uint32_t psw;            /* All psw bits except the following:  */ uint32_t psw_xb;         /* X and B, in their normal positions */ target_ulong psw_n;      /* boolean */

target/hppa/cpu.h +0 -0
@@ -294,7 +295,7 @@ struct HPPACPUClass {
 
Quoted 9 lines

target/hppa/cpu.h struct HPPACPUClass { · line 299

Quoted 5 lines
target/hppa/cpu.h +1 -1
 static inline bool hppa_is_pa20(CPUHPPAState *env)
 {
-    return object_dynamic_cast(OBJECT(env_cpu(env)), TYPE_HPPA64_CPU) != NULL;
+    return env->is_pa20;
 }

Now this function name is longer than what it extends to so maybe it would be simpler to drop the inline function and use env->is_pa20 directly where it's needed?

Yes, that's a possible cleanup which can be done afterwards.

Quoted 1 line

Is there a reason to keep the function?

Personally I like it more than the "env->is_pa20".
Richard, any opinion from your side? Should I send a such a replacement patch?

Helge

#3 lore raw
Quoted 11 lines · Helge Deller
Quoted 9 lines · BALATON Zoltan
Quoted 5 lines

static inline bool hppa_is_pa20(CPUHPPAState *env)
{
-    return object_dynamic_cast(OBJECT(env_cpu(env)), TYPE_HPPA64_CPU) != NULL; +    return env->is_pa20;
}

Now this function name is longer than what it extends to so maybe it would be simpler to drop the inline function and use env->is_pa20 directly where it's needed?

Quoted 6 lines

Yes, that's a possible cleanup which can be done afterwards.

Quoted 1 line

Is there a reason to keep the function?

Personally I like it more than the "env->is_pa20".
Richard, any opinion from your side? Should I send a such a replacement patch?

I like keeping the accessor function.

r~

#4 lore raw
In reply to Helge Deller
Quoted 13 lines · Helge Deller

Although the hppa_is_pa20() helper is costly due to string comparisms in object_dynamic_cast(), it is called quite often during memory lookups and at each start of a block of instruction translations. Speed hppa_is_pa20() up by calling object_dynamic_cast() only once at CPU creation and store the result in the is_pa20 of struct CPUArchState.

hw/hppa/machine.c +0 -0
index a31dc32a9f..08ac1ec068 100644

hw/hppa/machine.c static TranslateFn *machine_HP_common_init_cpus(MachineState *machine) · line 285

Quoted 5 lines
hw/hppa/machine.c +1 -0
@@ -281,6 +281,7 @@ static TranslateFn *machine_HP_common_init_cpus(MachineState *machine)
      /* Create CPUs.  */
      for (unsigned int i = 0; i < smp_cpus; i++) {
          cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
+        cpu[i]->env.is_pa20 = object_dynamic_cast(OBJECT(cpu[i]), TYPE_HPPA64_CPU);
      }

This belongs in hppa_cpu_initfn, since it's internal to the workings of the cpu. Otherwise,

target/hppa/cpu.h typedef struct CPUArchState { · line 211

Quoted 9 lines
target/hppa/cpu.h +1 -0
index e45ba50a59..c37a701f44 100644
@@ -208,6 +208,7 @@ typedef struct CPUArchState {
      uint64_t fr[32];
      uint64_t sr[8];          /* stored shifted into place for gva */
 
+    bool is_pa20;

This placement will interact badly with your reset function. Probably better to put it at the end of ArchCPU.

r~