public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/ARM/heads/morello)] fold-const: Fix optimization of COND_EXPR with capabilities
@ 2022-02-28 12:09 Matthew Malcomson
  0 siblings, 0 replies; only message in thread
From: Matthew Malcomson @ 2022-02-28 12:09 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:060ab6aa2f69fc61fb35c598026917bee7269ff9

commit 060ab6aa2f69fc61fb35c598026917bee7269ff9
Author: Alex Coplan <alex.coplan@arm.com>
Date:   Wed Feb 23 14:11:22 2022 +0000

    fold-const: Fix optimization of COND_EXPR with capabilities
    
    fold_ternary_loc tries to optimize A ? 0 : 1 into !A. To do this, it
    negates the condition and then tries to fold_convert_loc the condition
    into the result type, which may be a capability type. It is of course
    invalid to try and just convert a non-capability type into a capability
    type. Instead, in this case, we need to null-derive a capability using
    REPLACE_ADDRESS_VALUE.
    
    gcc/ChangeLog:
    
            * fold-const.c (fold_ternary_loc): Fix A ? 0 : 1 -> !A
            optimization for capabilities.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/aarch64/morello/fold-ternary.c: New test.

Diff:
---
 gcc/fold-const.c                                        | 13 +++++++++----
 gcc/testsuite/gcc.target/aarch64/morello/fold-ternary.c |  3 +++
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index ec11bf463d8..c593bb9a74c 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -12474,10 +12474,15 @@ fold_ternary_loc (location_t loc, enum tree_code code, tree type,
 	  && integer_onep (op2)
 	  && !VECTOR_TYPE_P (type)
 	  && truth_value_p (TREE_CODE (arg0)))
-	return pedantic_non_lvalue_loc (loc,
-				    fold_convert_loc (loc, type,
-					      invert_truthvalue_loc (loc,
-								     arg0)));
+	{
+	  tem = fold_convert_loc (loc, noncapability_type (type),
+				  invert_truthvalue_loc (loc, arg0));
+	  if (capability_type_p (type))
+	    tem = fold_build_replace_address_value_loc (loc,
+							build_int_cst (type, 0),
+							tem);
+	  return pedantic_non_lvalue_loc (loc, tem);
+	}
 
       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
       if (TREE_CODE (arg0) == LT_EXPR
diff --git a/gcc/testsuite/gcc.target/aarch64/morello/fold-ternary.c b/gcc/testsuite/gcc.target/aarch64/morello/fold-ternary.c
new file mode 100644
index 00000000000..59f9b6810cb
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/morello/fold-ternary.c
@@ -0,0 +1,3 @@
+/* { dg-do compile } */
+int a();
+void *b() { return a() ? 0 : (void *)1; }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-02-28 12:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-28 12:09 [gcc(refs/vendors/ARM/heads/morello)] fold-const: Fix optimization of COND_EXPR with capabilities Matthew Malcomson

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).