public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Nathan Sidwell <nathan@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/c++-modules] analyzer: fix pattern-test-2.c (PR 93291)
Date: Fri, 31 Jan 2020 17:43:00 -0000	[thread overview]
Message-ID: <20200131174313.2912.qmail@sourceware.org> (raw)

https://gcc.gnu.org/g:f89e844f9c589d1717248597641d25d81a4acb41

commit f89e844f9c589d1717248597641d25d81a4acb41
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Fri Jan 17 11:03:15 2020 -0500

    analyzer: fix pattern-test-2.c (PR 93291)
    
    Amongst the inputs to the analyzer state machines that can lead to state
    transitions are conditions on CFG edges, such as a test for a pointer
    being non-NULL.
    
    These conditionals can be non-trivial to determine in the face of
    optimization.  For example, at -O2:
    
      if (p == NULL || q == NULL)
    
    is optimized on some targets (e.g. x86_64) to a bitwise-or:
    
      _1 = p_5(D) == 0B;
      _2 = q_6(D) == 0B;
      _3 = _1 | _2;
      if (_3 != 0)
        goto <bb 4>; [51.12%]
      else
        goto <bb 3>; [48.88%]
    
    but on other targets (e.g. powerpc64le) as control flow:
    
      if (p_2(D) == 0B)
        goto <bb 5>; [18.09%]
      else
        goto <bb 3>; [81.91%]
    
      <bb 3> [local count: 879501929]:
      if (q_3(D) == 0B)
        goto <bb 5>; [30.95%]
      else
        goto <bb 4>; [69.05%]
    
    region_model::add_any_constraints_from_ssa_def_stmt attempts to walk
    SSA def chains to reconstruct the conditions that hold, so that
    e.g. in the above case of bitwise-or, the state machine for
    "p" can transition to the "known-null" state along the edge leading
    to bb 3.
    
    In gcc.dg/analyzer/pattern-test-2.c I attempted to write test coverage
    for this, but the test fails on those targets for which the || is
    expressed via control flow.
    
    This patch rewrites the test to make explicit use of bitwise-or, and
    adds coverage for bitwise-and for good measure.
    
    gcc/testsuite/ChangeLog:
    	PR analyzer/93291
    	* gcc.dg/analyzer/pattern-test-2.c: Remove include of stdlib.h.
    	(test_2): Rewrite to explicitly perform a bitwise-or of two
    	boolean conditions.
    	(test_3): New function, to test bitwise-and.

Diff:
---
 gcc/testsuite/ChangeLog                        |  8 +++++++
 gcc/testsuite/gcc.dg/analyzer/pattern-test-2.c | 30 +++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 4359aa1..56acbd9 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,11 @@
+2020-01-27  David Malcolm  <dmalcolm@redhat.com>
+
+	PR analyzer/93291
+	* gcc.dg/analyzer/pattern-test-2.c: Remove include of stdlib.h.
+	(test_2): Rewrite to explicitly perform a bitwise-or of two
+	boolean conditions.
+	(test_3): New function, to test bitwise-and.
+
 2020-01-27  Richard Sandiford  <richard.sandiford@arm.com>
 
 	PR testsuite/71727
diff --git a/gcc/testsuite/gcc.dg/analyzer/pattern-test-2.c b/gcc/testsuite/gcc.dg/analyzer/pattern-test-2.c
index ffc06a2..f5424f5 100644
--- a/gcc/testsuite/gcc.dg/analyzer/pattern-test-2.c
+++ b/gcc/testsuite/gcc.dg/analyzer/pattern-test-2.c
@@ -1,8 +1,6 @@
 /* { dg-additional-options "-fanalyzer-checker=pattern-test -O2" } */
 // TODO: run this at every optimization level
 
-#include <stdlib.h>
-
 extern void foo(void *);
 extern void bar(void *);
 
@@ -18,12 +16,38 @@ void test1(void *ptr)
 
 void test_2 (void *p, void *q)
 {
-  if (p == NULL || q == NULL) /* { dg-line cond_2 }  */
+  _Bool tmp1 = p == 0;
+  _Bool tmp2 = q == 0;
+  _Bool tmp = tmp1 | tmp2;
+
+  /* Verify that we can detect the implied conditions on p and q here.  */
+  if (tmp) /* { dg-line cond_2 }  */
     return;
   foo(p);
 
+  /* { dg-warning "pattern match on 'tmp1 == 0'" "tmp1 == 0" { target *-*-* } cond_2 } */
+  /* { dg-warning "pattern match on 'tmp2 == 0'" "tmp2 == 0" { target *-*-* } cond_2 } */
   /* { dg-warning "pattern match on '<unknown> == 0'" "<unknown> == 0" { target *-*-* } cond_2 } */
   /* { dg-warning "pattern match on '<unknown> != 0'" "<unknown> != 0" { target *-*-* } cond_2 } */
   /* { dg-warning "pattern match on 'p != 0'" "p != 0" { target *-*-* } cond_2 } */
   /* { dg-warning "pattern match on 'q != 0'" "q != 0" { target *-*-* } cond_2 } */
 }
+
+void test_3 (void *p, void *q)
+{
+  _Bool tmp1 = p == 0;
+  _Bool tmp2 = q == 0;
+  _Bool tmp = tmp1 & tmp2;
+
+  /* Verify that we can detect the implied conditions on p and q here.  */
+  if (tmp) /* { dg-line cond_3 }  */
+    return;
+  foo(p);
+
+  /* { dg-warning "pattern match on 'tmp1 != 0'" "tmp1 != 0" { target *-*-* } cond_3 } */
+  /* { dg-warning "pattern match on 'tmp2 != 0'" "tmp2 != 0" { target *-*-* } cond_3 } */
+  /* { dg-warning "pattern match on '<unknown> == 0'" "<unknown> == 0" { target *-*-* } cond_3 } */
+  /* { dg-warning "pattern match on '<unknown> != 0'" "<unknown> != 0" { target *-*-* } cond_3 } */
+  /* { dg-warning "pattern match on 'p == 0'" "p == 0" { target *-*-* } cond_3 } */
+  /* { dg-warning "pattern match on 'q == 0'" "q == 0" { target *-*-* } cond_3 } */
+}


                 reply	other threads:[~2020-01-31 17:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200131174313.2912.qmail@sourceware.org \
    --to=nathan@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).