diff mbox series

[RFC] Add gcc_assert_implies

Message ID ce32de23-0b57-46dd-a26e-f141c38f69e6@suse.de
State New
Headers show
Series [RFC] Add gcc_assert_implies | expand

Commit Message

Tom de Vries Jan. 6, 2019, 11:03 a.m. UTC
Hi,

Comments welcome.

Thanks,
- Tom

Comments

Richard Biener Jan. 7, 2019, 8:13 a.m. UTC | #1
On Sun, 6 Jan 2019, Tom de Vries wrote:

> Hi,
> 
> Comments welcome.

I find it harder to understand the gcc_assert_implies variant...
maybe because I'm not used to seeing it.

Richard.

> Thanks,
> - Tom
>
diff mbox series

Patch

[RFC] Add gcc_assert_implies

[ Following up on "Fix bug in simplify_ternary_operation" (
https://gcc.gnu.org/ml/gcc-patches/2017-11/msg01699.html ). ]

The C/C++ languages do not support the implication logical operator a -> b, so
we typically use '!a || b'.  Or, possibly the equivalent but more complicated
'!(a && !b)'.  Both have the convenient property that b is not evaluated if !a.

However, for both forms, the more complex and/or long operands a and b become,
the harder it becomes to understand that there's an implication relationship.

Add a macro gcc_assert_implies(a, b) that allows us to express a top-level
implication relationship in an assert.

The semantics of gcc_assert_implies (a, b) can understood as:
...
  if (a)
    gcc_assert (b);
...

[ So an alternative name could be gcc_assert_if, gcc_if_assert or
gcc_cond_assert. ]

Also, using this kind of assert will make it easier to spot that:
...
  gcc_assert_implies (a, b);
  if (a)
    {
      ...
    }
...
can be simplified into:
...
  if (a)
    {
      gcc_assert (b);
      ...
    }
...

2019-01-06  Tom de Vries  <tdevries@suse.de>

	* system.h (gcc_assert_implies): Define.
	* gimple-iterator.c (gsi_insert_seq_nodes_before): Use
	gcc_assert_implies.
	* simplify-rtx.c (simplify_const_relational_operation): Same.

---
 gcc/gimple-iterator.c | 2 +-
 gcc/simplify-rtx.c    | 5 ++---
 gcc/system.h          | 2 ++
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/gcc/gimple-iterator.c b/gcc/gimple-iterator.c
index e0e4e123678..2e55ec00c0c 100644
--- a/gcc/gimple-iterator.c
+++ b/gcc/gimple-iterator.c
@@ -119,7 +119,7 @@  gsi_insert_seq_nodes_before (gimple_stmt_iterator *i,
   basic_block bb;
   gimple_seq_node cur = i->ptr;
 
-  gcc_assert (!cur || cur->prev);
+  gcc_assert_implies (cur, cur->prev);
 
   if ((bb = gsi_bb (*i)) != NULL)
     update_bb_for_stmts (first, last, bb);
diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c
index 748155a5823..eabe6eeb281 100644
--- a/gcc/simplify-rtx.c
+++ b/gcc/simplify-rtx.c
@@ -5234,9 +5234,8 @@  simplify_const_relational_operation (enum rtx_code code,
   rtx trueop0;
   rtx trueop1;
 
-  gcc_assert (mode != VOIDmode
-	      || (GET_MODE (op0) == VOIDmode
-		  && GET_MODE (op1) == VOIDmode));
+  gcc_assert_implies (mode == VOIDmode,
+		      GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode);
 
   /* If op0 is a compare, extract the comparison arguments from it.  */
   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
diff --git a/gcc/system.h b/gcc/system.h
index d04f8fd3360..abbb121a8b4 100644
--- a/gcc/system.h
+++ b/gcc/system.h
@@ -748,6 +748,8 @@  extern void fancy_abort (const char *, int, const char *)
 #define gcc_assert(EXPR) ((void)(0 && (EXPR)))
 #endif
 
+#define gcc_assert_implies(EXPR1, EXPR2) gcc_assert (!(EXPR1) || (EXPR2))
+
 #if CHECKING_P
 #define gcc_checking_assert(EXPR) gcc_assert (EXPR)
 #else