[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 05/29] scripts/coccinelle: Add cpu_env.cocci script
From: |
Philippe Mathieu-Daudé |
Subject: |
[PATCH v3 05/29] scripts/coccinelle: Add cpu_env.cocci script |
Date: |
Mon, 29 Jan 2024 17:44:47 +0100 |
Add a Coccinelle script to convert the following slow path
(due to the QOM cast macro):
&ARCH_CPU(..)->env
to the following fast path:
cpu_env(..)
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
MAINTAINERS | 1 +
scripts/coccinelle/cpu_env.cocci | 100 +++++++++++++++++++++++++++++++
2 files changed, 101 insertions(+)
create mode 100644 scripts/coccinelle/cpu_env.cocci
diff --git a/MAINTAINERS b/MAINTAINERS
index dfaca8323e..ca3c8c18ab 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -157,6 +157,7 @@ F: accel/tcg/
F: accel/stubs/tcg-stub.c
F: util/cacheinfo.c
F: util/cacheflush.c
+F: scripts/coccinelle/cpu_env.cocci
F: scripts/decodetree.py
F: docs/devel/decodetree.rst
F: docs/devel/tcg*
diff --git a/scripts/coccinelle/cpu_env.cocci b/scripts/coccinelle/cpu_env.cocci
new file mode 100644
index 0000000000..5a70c2211a
--- /dev/null
+++ b/scripts/coccinelle/cpu_env.cocci
@@ -0,0 +1,100 @@
+/*
+ * Convert &ARCH_CPU(..)->env to use cpu_env(..).
+ *
+ * Rationale: ARCH_CPU() might be slow, being a QOM cast macro.
+ * cpu_env() is its fast equivalent.
+ * CPU() macro is a no-op.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ * SPDX-FileCopyrightText: Linaro Ltd 2024
+ * SPDX-FileContributor: Philippe Mathieu-Daudé
+ */
+
+@@
+type ArchCPU =~ "CPU$";
+identifier cpu;
+type CPUArchState =~ "^CPU";
+identifier env;
+@@
+ ArchCPU *cpu;
+ ...
+ CPUArchState *env = &cpu->env;
+ <...
+- &cpu->env
++ env
+ ...>
+
+
+/*
+ * Due to commit 8ce5c64499 ("semihosting: Return failure from
+ * softmmu-uaccess.h functions"), skip functions using softmmu-uaccess.h
+ * macros (they don't pass 'env' as argument).
+ */
+@ uaccess_api_used exists @
+identifier semihosting_func =~ "^(put|get)_user_[us](al|8|16|32)$";
+@@
+ semihosting_func(...)
+
+
+/*
+ * Argument is CPUState*
+ */
+@ cpustate_arg depends on !uaccess_api_used @
+identifier cpu;
+type ArchCPU =~ "CPU$";
+type CPUArchState;
+identifier ARCH_CPU =~ "CPU$";
+identifier env;
+CPUState *cs;
+@@
+- ArchCPU *cpu = ARCH_CPU(cs);
+ ...
+- CPUArchState *env = &cpu->env;
++ CPUArchState *env = cpu_env(cs);
+ ... when != cpu
+
+
+/*
+ * Argument is not CPUState* but a related QOM object.
+ * CPU() is not a QOM macro but a cast (See commit 0d6d1ab499).
+ */
+@ depends on !uaccess_api_used && !cpustate_arg @
+identifier cpu;
+type ArchCPU =~ "CPU$";
+type CPUArchState;
+identifier ARCH_CPU =~ "CPU$";
+identifier env;
+expression cs;
+@@
+- ArchCPU *cpu = ARCH_CPU(cs);
+ ...
+- CPUArchState *env = &cpu->env;
++ CPUArchState *env = cpu_env(CPU(cs));
+ ... when != cpu
+
+
+/* When single use of 'env', call cpu_env() in place */
+@ depends on !uaccess_api_used @
+type CPUArchState;
+identifier env;
+expression cs;
+@@
+- CPUArchState *env = cpu_env(cs);
+ ... when != env
+- env
++ cpu_env(cs)
+ ... when != env
+
+
+/* Both first_cpu/current_cpu are extern CPUState* */
+@@
+symbol first_cpu;
+symbol current_cpu;
+@@
+(
+- CPU(first_cpu)
++ first_cpu
+|
+- CPU(current_cpu)
++ current_cpu
+)
--
2.41.0
- [PATCH v3 00/29] hw, target: Prefer fast cpu_env() over slower CPU QOM cast macro, Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 01/29] bulk: Access existing variables initialized to &S->F when available, Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 02/29] hw/core: Declare CPUArchId::cpu as CPUState instead of Object, Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 03/29] hw/acpi/cpu: Use CPUState typedef, Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 04/29] bulk: Call in place single use cpu_env(), Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 05/29] scripts/coccinelle: Add cpu_env.cocci script,
Philippe Mathieu-Daudé <=
- [PATCH v3 07/29] target/alpha: Prefer fast cpu_env() over slower CPU QOM cast macro, Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 09/29] target/avr: Prefer fast cpu_env() over slower CPU QOM cast macro, Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 06/29] target: Replace CPU_GET_CLASS(cpu -> obj) in cpu_reset_hold() handler, Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 11/29] target/hexagon: Prefer fast cpu_env() over slower CPU QOM cast macro, Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 12/29] target/hppa: Prefer fast cpu_env() over slower CPU QOM cast macro, Philippe Mathieu-Daudé, 2024/01/29
- [PATCH v3 13/29] target/i386/hvf: Use CPUState typedef, Philippe Mathieu-Daudé, 2024/01/29