Fortran: simplification of FINDLOC for constant complex arguments [PR110585]

Message ID trinity-5247d438-212e-429f-ac48-fefeeda3f07c-1688754739987@3c-app-gmx-bs47
State Accepted
Headers
Series Fortran: simplification of FINDLOC for constant complex arguments [PR110585] |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Harald Anlauf July 7, 2023, 6:32 p.m. UTC
  Dear all,

I intend to commit the attached obvious patch within 24h unless
someone objects.  gfc_compare_expr() did not handle the case of
complex constants, which may be compared for equality.  This
case is needed in the simplification of the FINDLOC intrinsic.

Regtested on x86_64-pc-linux-gnu.

Thanks,
Harald
  

Comments

Paul Richard Thomas July 8, 2023, 7:35 a.m. UTC | #1
Hi Harald,

This is indeed obvious :-)

Thanks for the patch.

Paul

On Fri, 7 Jul 2023 at 19:32, Harald Anlauf via Fortran
<fortran@gcc.gnu.org> wrote:
>
> Dear all,
>
> I intend to commit the attached obvious patch within 24h unless
> someone objects.  gfc_compare_expr() did not handle the case of
> complex constants, which may be compared for equality.  This
> case is needed in the simplification of the FINDLOC intrinsic.
>
> Regtested on x86_64-pc-linux-gnu.
>
> Thanks,
> Harald
>
  

Patch

From b6c4f70d2dac4863335874f0bd3486ea7db348d7 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Fri, 7 Jul 2023 20:25:06 +0200
Subject: [PATCH] Fortran: simplification of FINDLOC for constant complex
 arguments [PR110585]

gcc/fortran/ChangeLog:

	PR fortran/110585
	* arith.cc (gfc_compare_expr): Handle equality comparison of constant
	complex gfc_expr arguments.

gcc/testsuite/ChangeLog:

	PR fortran/110585
	* gfortran.dg/findloc_9.f90: New test.
---
 gcc/fortran/arith.cc                    |  5 +++++
 gcc/testsuite/gfortran.dg/findloc_9.f90 | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/findloc_9.f90

diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc
index 86d56406047..f9c6658f860 100644
--- a/gcc/fortran/arith.cc
+++ b/gcc/fortran/arith.cc
@@ -1120,6 +1120,11 @@  gfc_compare_expr (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
 	    || (op1->value.logical && !op2->value.logical));
       break;

+    case BT_COMPLEX:
+      gcc_assert (op == INTRINSIC_EQ);
+      rc = mpc_cmp (op1->value.complex, op2->value.complex);
+      break;
+
     default:
       gfc_internal_error ("gfc_compare_expr(): Bad basic type");
     }
diff --git a/gcc/testsuite/gfortran.dg/findloc_9.f90 b/gcc/testsuite/gfortran.dg/findloc_9.f90
new file mode 100644
index 00000000000..05974476cb3
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/findloc_9.f90
@@ -0,0 +1,19 @@ 
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+! PR fortran/110585 - simplification of FINDLOC for constant complex arguments
+
+program mvce
+  implicit none
+  integer, parameter :: a(*) = findloc([(1.,0.),(2.,1.)], (2.,0.))
+  integer, parameter :: b(*) = findloc([(1.,0.),(2.,1.)], (2.,0.), back=.true.)
+  integer, parameter :: c(*) = findloc([(1.,0.),(2.,1.)], (2.,1.))
+  integer, parameter :: d(*) = findloc([(1.,0.),(2.,1.)], (2.,1.), back=.true.)
+  integer, parameter :: e    = findloc([(1.,0.),(2.,1.)], (2.,1.), dim=1)
+  if (a(1) /= 0) stop 1
+  if (b(1) /= 0) stop 2
+  if (c(1) /= 2) stop 3
+  if (d(1) /= 2) stop 4
+  if (e    /= 2) stop 5
+end
+
+! { dg-final { scan-tree-dump-not "_gfortran_stop_numeric" "original" } }
--
2.35.3