gimplify: Handle void BIND_EXPR as asm input [PR100501]

Message ID 821087c6-907d-44ff-2d51-841a6cb0462e@redhat.com
State New
Headers
Series gimplify: Handle void BIND_EXPR as asm input [PR100501] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed

Commit Message

Joseph Myers Nov. 29, 2024, 2 a.m. UTC
  As reported in bug 100501 (plus duplicates), the gimplifier ICEs for C
tests involving a statement expression not returning a value as an asm
input.

The expected diagnostic for this case (as seen for C++ input) is one
coming from the gimplifier and so it seems reasonable to fix the
gimplifier to handle the GENERIC generated for this case by the C
front end, rather than trying to make the C front end detect it
earlier.  Thus, adjust two places in the gimplifier to work with
gimplifying a BIND_EXPR changing *expr_p to NULL_TREE.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.  OK to commit?

	PR c/100501

gcc/
	* gimplify.cc (gimplify_expr): Do not call gimple_test_f on
	*expr_p when it has become null.
	(gimplify_asm_expr): Handle TREE_VALUE (link) becoming null.

gcc/testsuite/
	* gcc.dg/pr100501-1.c: New test.
  

Comments

Richard Biener Nov. 29, 2024, 7:42 a.m. UTC | #1
On Fri, Nov 29, 2024 at 3:04 AM Joseph Myers <josmyers@redhat.com> wrote:
>
> As reported in bug 100501 (plus duplicates), the gimplifier ICEs for C
> tests involving a statement expression not returning a value as an asm
> input.
>
> The expected diagnostic for this case (as seen for C++ input) is one
> coming from the gimplifier and so it seems reasonable to fix the
> gimplifier to handle the GENERIC generated for this case by the C
> front end, rather than trying to make the C front end detect it
> earlier.  Thus, adjust two places in the gimplifier to work with
> gimplifying a BIND_EXPR changing *expr_p to NULL_TREE.
>
> Bootstrapped with no regressions for x86_64-pc-linux-gnu.  OK to commit?
>
>         PR c/100501
>
> gcc/
>         * gimplify.cc (gimplify_expr): Do not call gimple_test_f on
>         *expr_p when it has become null.
>         (gimplify_asm_expr): Handle TREE_VALUE (link) becoming null.
>
> gcc/testsuite/
>         * gcc.dg/pr100501-1.c: New test.
>
> diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
> index fb0ca23bfb6c..090f8987d5d3 100644
> --- a/gcc/gimplify.cc
> +++ b/gcc/gimplify.cc
> @@ -7457,6 +7457,13 @@ gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
>             TREE_VALUE (link) = error_mark_node;
>           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
>                                 is_gimple_lvalue, fb_lvalue | fb_mayfail);
> +         if (TREE_VALUE (link) == NULL_TREE)

I think we're trying to handle errorneous cases by setting TREE_VALUE
to error_mark_node
before this, so how about the following instead?

diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index fb0ca23bfb6..aa99c0a98f7 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -7453,7 +7453,8 @@ gimplify_asm_expr (tree *expr_p, gimple_seq
*pre_p, gimple_seq *post_p)
              || TREE_CODE (inputv) == PREINCREMENT_EXPR
              || TREE_CODE (inputv) == POSTDECREMENT_EXPR
              || TREE_CODE (inputv) == POSTINCREMENT_EXPR
-             || TREE_CODE (inputv) == MODIFY_EXPR)
+             || TREE_CODE (inputv) == MODIFY_EXPR
+             || VOID_TYPE_P (TREE_TYPE (inputv)))
            TREE_VALUE (link) = error_mark_node;
          tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
                                is_gimple_lvalue, fb_lvalue | fb_mayfail);


> +           {
> +             /* This can occur when an asm input is a BIND_EXPR for a
> +                statement expression not returning a value.  */
> +             tret = GS_ERROR;
> +             TREE_VALUE (link) = error_mark_node;
> +           }
>           if (tret != GS_ERROR)
>             {
>               /* Unlike output operands, memory inputs are not guaranteed
> @@ -19662,10 +19669,11 @@ gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
>
>    /* Otherwise we're gimplifying a subexpression, so the resulting
>       value is interesting.  If it's a valid operand that matches
> -     GIMPLE_TEST_F, we're done. Unless we are handling some
> -     post-effects internally; if that's the case, we need to copy into
> -     a temporary before adding the post-effects to POST_P.  */
> -  if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
> +     GIMPLE_TEST_F, or it's now NULL_TREE, we're done.  Unless we are
> +     handling some post-effects internally; if that's the case, we need
> +     to copy into a temporary before adding the post-effects to POST_P.  */
> +  if (gimple_seq_empty_p (internal_post)
> +      && (!*expr_p || (*gimple_test_f) (*expr_p)))
>      goto out;
>
>    /* Otherwise, we need to create a new temporary for the gimplified
> diff --git a/gcc/testsuite/gcc.dg/pr100501-1.c b/gcc/testsuite/gcc.dg/pr100501-1.c
> new file mode 100644
> index 000000000000..b5b3781a9c2f
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr100501-1.c
> @@ -0,0 +1,26 @@
> +/* Test ICE for statement expression returning no value as asm input (bug
> +   100501).  */
> +/* { dg-do compile } */
> +/* { dg-options "" } */
> +
> +int x;
> +int g ();
> +
> +void
> +f ()
> +{
> +  __asm__ ("" : : "m" (({}))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ ; }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ (void) 0; }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ f (); }))); /* { dg-error "memory input 0 is not directly addressable|using result of function returning 'void'" } */
> +  __asm__ ("" : : "m" (({ f (); f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ x = g (); f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ if (1) g (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ if (1) g (); else g (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ test : goto test; }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ return; }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ while (1); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ do {} while (1); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ for (;;); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ switch (x); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +}
>
> --
> Joseph S. Myers
> josmyers@redhat.com
>
  
Joseph Myers Nov. 29, 2024, 10:45 p.m. UTC | #2
On Fri, 29 Nov 2024, Richard Biener wrote:

> I think we're trying to handle errorneous cases by setting TREE_VALUE
> to error_mark_node
> before this, so how about the following instead?

Yes, that works, and also fixes the test in bug 100792 unlike my previous 
patch.  Here's a full, tested patch using your version.


gimplify: Handle void expression as asm input [PR100501, PR100792]

As reported in bug 100501 (plus duplicates), the gimplifier ICEs for C
tests involving a statement expression not returning a value as an asm
input; this includes the variant bug 100792 where the statement
expression ends with another asm statement.

The expected diagnostic for this case (as seen for C++ input) is one
coming from the gimplifier and so it seems reasonable to fix the
gimplifier to handle the GENERIC generated for this case by the C
front end, rather than trying to make the C front end detect it
earlier.  Thus the gimplifier to handle a void
expression like other non-lvalues for such a memory input.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.  OK to commit?

	PR c/100501
	PR c/100792

gcc/
	* gimplify.cc (gimplify_asm_expr): Handle void expressions for
	memory inputs like other non-lvalues.

gcc/testsuite/
	* gcc.dg/pr100501-1.c, gcc.dg/pr100792-1.c: New tests.
	* gcc.dg/pr48552-1.c, gcc.dg/pr48552-2.c,
	gcc.dg/torture/pr98601.c: Update expected errors.

Co-authored-by: Richard Biener <rguenther@suse.de>

diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index fb0ca23bfb6c..aa99c0a98f73 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -7453,7 +7453,8 @@ gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
 	      || TREE_CODE (inputv) == PREINCREMENT_EXPR
 	      || TREE_CODE (inputv) == POSTDECREMENT_EXPR
 	      || TREE_CODE (inputv) == POSTINCREMENT_EXPR
-	      || TREE_CODE (inputv) == MODIFY_EXPR)
+	      || TREE_CODE (inputv) == MODIFY_EXPR
+	      || VOID_TYPE_P (TREE_TYPE (inputv)))
 	    TREE_VALUE (link) = error_mark_node;
 	  tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
 				is_gimple_lvalue, fb_lvalue | fb_mayfail);
diff --git a/gcc/testsuite/gcc.dg/pr100501-1.c b/gcc/testsuite/gcc.dg/pr100501-1.c
new file mode 100644
index 000000000000..152caac8b5d1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr100501-1.c
@@ -0,0 +1,26 @@
+/* Test ICE for statement expression returning no value as asm input (bug
+   100501).  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int x;
+int g ();
+
+void
+f ()
+{
+  __asm__ ("" : : "m" (({}))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ ; }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ (void) 0; }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ f (); f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ x = g (); f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ if (1) g (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ if (1) g (); else g (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ test : goto test; }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ return; }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ while (1); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ do {} while (1); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ for (;;); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ switch (x); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+}
diff --git a/gcc/testsuite/gcc.dg/pr100792-1.c b/gcc/testsuite/gcc.dg/pr100792-1.c
new file mode 100644
index 000000000000..52f3aaf83f73
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr100792-1.c
@@ -0,0 +1,10 @@
+/* Test ICE for statement expression ending with asm as asm input (bug
+   100792).  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void
+f ()
+{
+  __asm__ ("" : : "m" (({ __asm__ (""); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+}
diff --git a/gcc/testsuite/gcc.dg/pr48552-1.c b/gcc/testsuite/gcc.dg/pr48552-1.c
index 4cd7c59011ef..b3ef453cb1d5 100644
--- a/gcc/testsuite/gcc.dg/pr48552-1.c
+++ b/gcc/testsuite/gcc.dg/pr48552-1.c
@@ -20,6 +20,7 @@ void
 f3 (void *x)
 {
   __asm volatile ("" : : "m" (*x));	/* { dg-warning "dereferencing" } */
+  /* { dg-error "memory input 0 is not directly addressable" "not addressable" { target *-*-* } .-1 } */
 }
 
 void
diff --git a/gcc/testsuite/gcc.dg/pr48552-2.c b/gcc/testsuite/gcc.dg/pr48552-2.c
index e22600a24763..003926ce631a 100644
--- a/gcc/testsuite/gcc.dg/pr48552-2.c
+++ b/gcc/testsuite/gcc.dg/pr48552-2.c
@@ -20,6 +20,7 @@ void
 f3 (void *x)
 {
   __asm ("" : : "m" (*x));	/* { dg-warning "dereferencing" } */
+  /* { dg-error "memory input 0 is not directly addressable" "not addressable" { target *-*-* } .-1 } */
 }
 
 void
diff --git a/gcc/testsuite/gcc.dg/torture/pr98601.c b/gcc/testsuite/gcc.dg/torture/pr98601.c
index ee9d076c02de..a57ee35fd455 100644
--- a/gcc/testsuite/gcc.dg/torture/pr98601.c
+++ b/gcc/testsuite/gcc.dg/torture/pr98601.c
@@ -11,4 +11,5 @@ void
 bar (void *p)
 {
   asm volatile ("" : : "m" (*p));	/* { dg-warning "dereferencing 'void \\*' pointer" } */
+  /* { dg-error "memory input 0 is not directly addressable" "not addressable" { target *-*-* } .-1 } */
 }
  
Richard Biener Nov. 30, 2024, 8:31 a.m. UTC | #3
> Am 29.11.2024 um 23:45 schrieb Joseph Myers <josmyers@redhat.com>:
> 
> On Fri, 29 Nov 2024, Richard Biener wrote:
> 
>> I think we're trying to handle errorneous cases by setting TREE_VALUE
>> to error_mark_node
>> before this, so how about the following instead?
> 
> Yes, that works, and also fixes the test in bug 100792 unlike my previous
> patch.  Here's a full, tested patch using your version.
> 
> 
> gimplify: Handle void expression as asm input [PR100501, PR100792]
> 
> As reported in bug 100501 (plus duplicates), the gimplifier ICEs for C
> tests involving a statement expression not returning a value as an asm
> input; this includes the variant bug 100792 where the statement
> expression ends with another asm statement.
> 
> The expected diagnostic for this case (as seen for C++ input) is one
> coming from the gimplifier and so it seems reasonable to fix the
> gimplifier to handle the GENERIC generated for this case by the C
> front end, rather than trying to make the C front end detect it
> earlier.  Thus the gimplifier to handle a void
> expression like other non-lvalues for such a memory input.
> 
> Bootstrapped with no regressions for x86_64-pc-linux-gnu.  OK to commit?

Ok

Richard 

>    PR c/100501
>    PR c/100792
> 
> gcc/
>    * gimplify.cc (gimplify_asm_expr): Handle void expressions for
>    memory inputs like other non-lvalues.
> 
> gcc/testsuite/
>    * gcc.dg/pr100501-1.c, gcc.dg/pr100792-1.c: New tests.
>    * gcc.dg/pr48552-1.c, gcc.dg/pr48552-2.c,
>    gcc.dg/torture/pr98601.c: Update expected errors.
> 
> Co-authored-by: Richard Biener <rguenther@suse.de>
> 
> diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
> index fb0ca23bfb6c..aa99c0a98f73 100644
> --- a/gcc/gimplify.cc
> +++ b/gcc/gimplify.cc
> @@ -7453,7 +7453,8 @@ gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
>          || TREE_CODE (inputv) == PREINCREMENT_EXPR
>          || TREE_CODE (inputv) == POSTDECREMENT_EXPR
>          || TREE_CODE (inputv) == POSTINCREMENT_EXPR
> -          || TREE_CODE (inputv) == MODIFY_EXPR)
> +          || TREE_CODE (inputv) == MODIFY_EXPR
> +          || VOID_TYPE_P (TREE_TYPE (inputv)))
>        TREE_VALUE (link) = error_mark_node;
>      tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
>                is_gimple_lvalue, fb_lvalue | fb_mayfail);
> diff --git a/gcc/testsuite/gcc.dg/pr100501-1.c b/gcc/testsuite/gcc.dg/pr100501-1.c
> new file mode 100644
> index 000000000000..152caac8b5d1
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr100501-1.c
> @@ -0,0 +1,26 @@
> +/* Test ICE for statement expression returning no value as asm input (bug
> +   100501).  */
> +/* { dg-do compile } */
> +/* { dg-options "" } */
> +
> +int x;
> +int g ();
> +
> +void
> +f ()
> +{
> +  __asm__ ("" : : "m" (({}))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ ; }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ (void) 0; }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ f (); f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ x = g (); f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ if (1) g (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ if (1) g (); else g (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ test : goto test; }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ return; }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ while (1); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ do {} while (1); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ for (;;); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +  __asm__ ("" : : "m" (({ switch (x); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +}
> diff --git a/gcc/testsuite/gcc.dg/pr100792-1.c b/gcc/testsuite/gcc.dg/pr100792-1.c
> new file mode 100644
> index 000000000000..52f3aaf83f73
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr100792-1.c
> @@ -0,0 +1,10 @@
> +/* Test ICE for statement expression ending with asm as asm input (bug
> +   100792).  */
> +/* { dg-do compile } */
> +/* { dg-options "" } */
> +
> +void
> +f ()
> +{
> +  __asm__ ("" : : "m" (({ __asm__ (""); }))); /* { dg-error "memory input 0 is not directly addressable" } */
> +}
> diff --git a/gcc/testsuite/gcc.dg/pr48552-1.c b/gcc/testsuite/gcc.dg/pr48552-1.c
> index 4cd7c59011ef..b3ef453cb1d5 100644
> --- a/gcc/testsuite/gcc.dg/pr48552-1.c
> +++ b/gcc/testsuite/gcc.dg/pr48552-1.c
> @@ -20,6 +20,7 @@ void
> f3 (void *x)
> {
>   __asm volatile ("" : : "m" (*x));    /* { dg-warning "dereferencing" } */
> +  /* { dg-error "memory input 0 is not directly addressable" "not addressable" { target *-*-* } .-1 } */
> }
> 
> void
> diff --git a/gcc/testsuite/gcc.dg/pr48552-2.c b/gcc/testsuite/gcc.dg/pr48552-2.c
> index e22600a24763..003926ce631a 100644
> --- a/gcc/testsuite/gcc.dg/pr48552-2.c
> +++ b/gcc/testsuite/gcc.dg/pr48552-2.c
> @@ -20,6 +20,7 @@ void
> f3 (void *x)
> {
>   __asm ("" : : "m" (*x));    /* { dg-warning "dereferencing" } */
> +  /* { dg-error "memory input 0 is not directly addressable" "not addressable" { target *-*-* } .-1 } */
> }
> 
> void
> diff --git a/gcc/testsuite/gcc.dg/torture/pr98601.c b/gcc/testsuite/gcc.dg/torture/pr98601.c
> index ee9d076c02de..a57ee35fd455 100644
> --- a/gcc/testsuite/gcc.dg/torture/pr98601.c
> +++ b/gcc/testsuite/gcc.dg/torture/pr98601.c
> @@ -11,4 +11,5 @@ void
> bar (void *p)
> {
>   asm volatile ("" : : "m" (*p));    /* { dg-warning "dereferencing 'void \\*' pointer" } */
> +  /* { dg-error "memory input 0 is not directly addressable" "not addressable" { target *-*-* } .-1 } */
> }
> 
> 
> --
> Joseph S. Myers
> josmyers@redhat.com
>
  

Patch

diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index fb0ca23bfb6c..090f8987d5d3 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -7457,6 +7457,13 @@  gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
 	    TREE_VALUE (link) = error_mark_node;
 	  tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
 				is_gimple_lvalue, fb_lvalue | fb_mayfail);
+	  if (TREE_VALUE (link) == NULL_TREE)
+	    {
+	      /* This can occur when an asm input is a BIND_EXPR for a
+		 statement expression not returning a value.  */
+	      tret = GS_ERROR;
+	      TREE_VALUE (link) = error_mark_node;
+	    }
 	  if (tret != GS_ERROR)
 	    {
 	      /* Unlike output operands, memory inputs are not guaranteed
@@ -19662,10 +19669,11 @@  gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
 
   /* Otherwise we're gimplifying a subexpression, so the resulting
      value is interesting.  If it's a valid operand that matches
-     GIMPLE_TEST_F, we're done. Unless we are handling some
-     post-effects internally; if that's the case, we need to copy into
-     a temporary before adding the post-effects to POST_P.  */
-  if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
+     GIMPLE_TEST_F, or it's now NULL_TREE, we're done.  Unless we are
+     handling some post-effects internally; if that's the case, we need
+     to copy into a temporary before adding the post-effects to POST_P.  */
+  if (gimple_seq_empty_p (internal_post)
+      && (!*expr_p || (*gimple_test_f) (*expr_p)))
     goto out;
 
   /* Otherwise, we need to create a new temporary for the gimplified
diff --git a/gcc/testsuite/gcc.dg/pr100501-1.c b/gcc/testsuite/gcc.dg/pr100501-1.c
new file mode 100644
index 000000000000..b5b3781a9c2f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr100501-1.c
@@ -0,0 +1,26 @@ 
+/* Test ICE for statement expression returning no value as asm input (bug
+   100501).  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int x;
+int g ();
+
+void
+f ()
+{
+  __asm__ ("" : : "m" (({}))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ ; }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ (void) 0; }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ f (); }))); /* { dg-error "memory input 0 is not directly addressable|using result of function returning 'void'" } */
+  __asm__ ("" : : "m" (({ f (); f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ x = g (); f (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ if (1) g (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ if (1) g (); else g (); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ test : goto test; }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ return; }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ while (1); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ do {} while (1); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ for (;;); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+  __asm__ ("" : : "m" (({ switch (x); }))); /* { dg-error "memory input 0 is not directly addressable" } */
+}