* [RFC] Add gcc_assert_implies
@ 2019-01-06 11:02 Tom de Vries
2019-01-07 8:13 ` Richard Biener
0 siblings, 1 reply; 2+ messages in thread
From: Tom de Vries @ 2019-01-06 11:02 UTC (permalink / raw)
To: gcc-patches; +Cc: Jeff Law, Richard Biener
[-- Attachment #1: Type: text/plain, Size: 38 bytes --]
Hi,
Comments welcome.
Thanks,
- Tom
[-- Attachment #2: 0001-RFC-Add-gcc_assert_implies.patch --]
[-- Type: text/x-patch, Size: 2892 bytes --]
[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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [RFC] Add gcc_assert_implies
2019-01-06 11:02 [RFC] Add gcc_assert_implies Tom de Vries
@ 2019-01-07 8:13 ` Richard Biener
0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2019-01-07 8:13 UTC (permalink / raw)
To: Tom de Vries; +Cc: gcc-patches, Jeff Law
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
>
--
Richard Biener <rguenther@suse.de>
SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nuernberg)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-01-07 8:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-06 11:02 [RFC] Add gcc_assert_implies Tom de Vries
2019-01-07 8:13 ` Richard Biener
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).