From f64eb636677d714781b4543f111b1c9239328db6 Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Thu, 28 Jul 2022 12:42:14 +0200
Subject: gimple, internal-fn: Add IFN_TRAP and use it for
 __builtin_unreachable [PR106099]

__builtin_unreachable and __ubsan_handle_builtin_unreachable don't
use vops, they are marked const/leaf/noreturn/nothrow/cold.
But __builtin_trap uses vops, isn't const, just leaf/noreturn/nothrow/cold.
This is I believe so that when users explicitly use __builtin_trap in their
sources they get stores visible at the trap side.
-fsanitize=unreachable -fsanitize-undefined-trap-on-error used to transform
__builtin_unreachable to __builtin_trap even in the past, but the sanopt pass
has TODO_update_ssa, so it worked fine.

Now that gimple_build_builtin_unreachable can build a __builtin_trap call
right away, we can run into problems that whenever we need it we would need
to either manually or through TODO_update* ensure the vops being updated.

Though, as it is originally __builtin_unreachable which is just implemented
as trap, I think for this case it is fine to avoid vops.  For this the
patch introduces IFN_TRAP, which has ECF_* flags like __builtin_unreachable
and is expanded as __builtin_trap.

2022-07-28  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/106099
	* internal-fn.def (TRAP): New internal fn.
	* internal-fn.h (expand_TRAP): Declare.
	* internal-fn.cc (expand_TRAP): Define.
	* gimple.cc (gimple_build_builtin_unreachable): For BUILT_IN_TRAP,
	use internal fn rather than builtin.

	* gcc.dg/ubsan/pr106099.c: New test.
---
 gcc/internal-fn.def | 4 ++++
 1 file changed, 4 insertions(+)

(limited to 'gcc/internal-fn.def')

diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def
index 7c398ba..7bf661c 100644
--- a/gcc/internal-fn.def
+++ b/gcc/internal-fn.def
@@ -456,6 +456,10 @@ DEF_INTERNAL_FN (SHUFFLEVECTOR, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL)
 /* <=> optimization.  */
 DEF_INTERNAL_FN (SPACESHIP, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL)
 
+/* __builtin_trap created from/for __builtin_unreachable.  */
+DEF_INTERNAL_FN (TRAP, ECF_CONST | ECF_LEAF | ECF_NORETURN
+		       | ECF_NOTHROW | ECF_COLD, NULL)
+
 #undef DEF_INTERNAL_INT_FN
 #undef DEF_INTERNAL_FLT_FN
 #undef DEF_INTERNAL_FLT_FLOATN_FN
-- 
cgit v1.1

