diff mbox series

[v4,03/16] qemu/compiler.h: Add optimize_away

Message ID 20190923230004.9231-4-richard.henderson@linaro.org
State New
Headers show
Series Move rom and notdirty handling to cputlb | expand

Commit Message

Richard Henderson Sept. 23, 2019, 10:59 p.m. UTC
Use this as a compile-time assert that a particular
code path is not reachable.

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

---
 include/qemu/compiler.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

-- 
2.17.1

Comments

David Hildenbrand Sept. 24, 2019, 7:47 a.m. UTC | #1
On 24.09.19 00:59, Richard Henderson wrote:
> Use this as a compile-time assert that a particular

> code path is not reachable.

> 

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

> ---

>  include/qemu/compiler.h | 15 +++++++++++++++

>  1 file changed, 15 insertions(+)

> 

> diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h

> index 20780e722d..6604ccea92 100644

> --- a/include/qemu/compiler.h

> +++ b/include/qemu/compiler.h

> @@ -221,4 +221,19 @@

>  #define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__))

>  #define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__))

>  

> +/**

> + * optimize_away()


I would have used the compiler-speak "optimized_out()" instead.

> + *

> + * The compiler, during optimization, is expected to prove that a call

> + * to this function cannot be reached and remove it.  If the compiler

> + * supports QEMU_ERROR, this will be reported at compile time; otherwise

> + * this will be reported at link time, due to the missing symbol.

> + */

> +#ifdef __OPTIMIZE__

> +extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")

> +    optimize_away(void);

> +#else

> +#define optimize_away()  g_assert_not_reached()

> +#endif

> +

>  #endif /* COMPILER_H */

> 


Apart from that looks good to me.

-- 

Thanks,

David / dhildenb
Alex Bennée Sept. 24, 2019, 3:47 p.m. UTC | #2
Richard Henderson <richard.henderson@linaro.org> writes:

> Use this as a compile-time assert that a particular

> code path is not reachable.

>

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


Reviewed-by: Alex Bennée <alex.bennee@linaro.org>


> ---

>  include/qemu/compiler.h | 15 +++++++++++++++

>  1 file changed, 15 insertions(+)

>

> diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h

> index 20780e722d..6604ccea92 100644

> --- a/include/qemu/compiler.h

> +++ b/include/qemu/compiler.h

> @@ -221,4 +221,19 @@

>  #define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__))

>  #define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__))

>

> +/**

> + * optimize_away()

> + *

> + * The compiler, during optimization, is expected to prove that a call

> + * to this function cannot be reached and remove it.  If the compiler

> + * supports QEMU_ERROR, this will be reported at compile time; otherwise

> + * this will be reported at link time, due to the missing symbol.

> + */

> +#ifdef __OPTIMIZE__

> +extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")

> +    optimize_away(void);

> +#else

> +#define optimize_away()  g_assert_not_reached()

> +#endif

> +

>  #endif /* COMPILER_H */



--
Alex Bennée
Richard Henderson Sept. 24, 2019, 5:27 p.m. UTC | #3
On 9/24/19 12:47 AM, David Hildenbrand wrote:
> On 24.09.19 00:59, Richard Henderson wrote:

>> +/**

>> + * optimize_away()

> 

> I would have used the compiler-speak "optimized_out()" instead.


Hmm, that's just a matter of present vs past test.

Perhaps, qemu_build_not_reached, to mirror g_assert_not_reached and
QEMU_BUILD_BUG_ON?


r~
David Hildenbrand Sept. 24, 2019, 5:29 p.m. UTC | #4
On 24.09.19 19:27, Richard Henderson wrote:
> On 9/24/19 12:47 AM, David Hildenbrand wrote:

>> On 24.09.19 00:59, Richard Henderson wrote:

>>> +/**

>>> + * optimize_away()

>>

>> I would have used the compiler-speak "optimized_out()" instead.

> 

> Hmm, that's just a matter of present vs past test.

> 

> Perhaps, qemu_build_not_reached, to mirror g_assert_not_reached and

> QEMU_BUILD_BUG_ON?

> 


Would also work for me.

-- 

Thanks,

David / dhildenb
diff mbox series

Patch

diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
index 20780e722d..6604ccea92 100644
--- a/include/qemu/compiler.h
+++ b/include/qemu/compiler.h
@@ -221,4 +221,19 @@ 
 #define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__))
 #define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__))
 
+/**
+ * optimize_away()
+ *
+ * The compiler, during optimization, is expected to prove that a call
+ * to this function cannot be reached and remove it.  If the compiler
+ * supports QEMU_ERROR, this will be reported at compile time; otherwise
+ * this will be reported at link time, due to the missing symbol.
+ */
+#ifdef __OPTIMIZE__
+extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")
+    optimize_away(void);
+#else
+#define optimize_away()  g_assert_not_reached()
+#endif
+
 #endif /* COMPILER_H */