public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: [PATCH] Fold VIEW_CONVERT_EXPR <type, STRING_CST> generated by  Fortran FE a lot (PR target/35366)
@ 2008-11-11 22:53 Tobias Burnus
  2008-11-12 14:20 ` Jakub Jelinek
  0 siblings, 1 reply; 28+ messages in thread
From: Tobias Burnus @ 2008-11-11 22:53 UTC (permalink / raw)
  To: gcc-patches, fortran

Hi,

Jakub wrote:
> So would the following be acceptable?  It will warn both for
> logical l
>   l = 4Hfoob
> and
>   l = transfer (42, .true.)

I think that would be sufficent. For completeness:
There is no warning given for
     b = transfer(i, b)
I think that is OK, but one could also give one as NAG f95 does:
  Warning: TRANSFER to LOGICAL might produce invalid value

I got the first replies to my c.l.f question,
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/cf874197dc460679
and reading Glen Herrmannsfeldt's and Richard Maine's reply,
there should be no problem:
- Holleriths were usually used with integer since real/logical
  are system dependent (e.g. normalization of REAL Holleriths)
- TRANSFER requires that the bit pattern matches
  (Richard: "I think to be a mistake"), but
  "even if you literally follow that requirement, there turn out to
   be gaping loopholes that pretty much prevent the user from being
   able to count on bit pattern preservation."
- Assignments, COMMON and EQUIVALENCE: No bit pattern needs to be
  preserved

Thus, your patch is OK from the standard and common practice point of
view.

       if (expr->representation.string)
	{
+	  if (TREE_CODE (tmp) == INTEGER_CST)
+	  else

I think there is no possibility to ever enter the else branch:
expr->representation.string only occurs for either Holleriths
or for TRANSFER of constants; I would go for a gcc_assert
instead.

The patch is OK from my side.


Brooks Moses wrote:
+		gfc_warning ("Assigning value other than 0 or 1 to LOGICAL"
+			     " at %L has undefined result", &expr->where);
> The warning could perhaps be edited a little, too, to reflect that the
> user isn't necessarily thinking of the input as an integer.  Maybe:
> "Cannot assign value with bitwise representation other than 0x0 or 0x1
> to LOGICAL at %L".

I find the original string clearer than especially the "0x0 or 0x1".

Tobias

^ permalink raw reply	[flat|nested] 28+ messages in thread
* [PATCH] Fold VIEW_CONVERT_EXPR <type, STRING_CST> generated by  Fortran FE a lot (PR target/35366)
@ 2008-11-11 13:27 Jakub Jelinek
  2008-11-11 15:40 ` Richard Guenther
  2008-11-11 15:53 ` Tobias Schlüter
  0 siblings, 2 replies; 28+ messages in thread
From: Jakub Jelinek @ 2008-11-11 13:27 UTC (permalink / raw)
  To: gcc-patches; +Cc: fortran

Hi!

Fortran FE generates VIEW_CONVERT_EXPR of STRING_CST quite a lot, but
fold-const isn't able to fold it, so GCC generates very inefficient
and (in some cases also buggy, as can be seen on the equiv_7.f90
on powerpc64-darwin with -m64 -Os) code.

I've bootstrapped/regtested this on x86_64-linux.

Unfortrunately, it causes 2 Fortran testsuite failures, hollerith.f90
and transfer_simplify_4.f90, both at all optimization levels.
I believe the tests are just invalid though.  The first one does:
logical l
l = 4Ho wo
and expects all the 32 bits preserved, but that is certainly against
the semantics of BOOLEAN_TYPE and from quick skimming of the Fortran
standard also LOGICAL type.  BOOLEAN_TYPE has just two values, false
and true (similarly for LOGICAL .false. and .true.) and so the folder
IMHO correctly folds this into
l = .true.
(4Ho wo is non-zero).  The transfer_simplify_4.f90 testcase transfers an
integer into logical and back and expects again all the 32-bits to be
preserved.

Fortran folks, can you please look at these 2 testcases and say whether
they are valid Fortran or just undefined behavior?

2008-11-11  Jakub Jelinek  <jakub@redhat.com>

	PR target/35366
	* fold-const.c (native_encode_string): New function.
	(native_encode_expr): Use it for STRING_CST.

	* gfortran.dg/hollerith.f90: Don't assume a 32-bit value
	stored into logical variable will be preserved.
	* gfortran.dg/transfer_simplify_4.f90: Removed.

--- gcc/fold-const.c.jj	2008-10-29 18:49:06.000000000 +0100
+++ gcc/fold-const.c	2008-11-11 11:41:34.000000000 +0100
@@ -7315,6 +7315,37 @@ native_encode_vector (const_tree expr, u
 }
 
 
+/* Subroutine of native_encode_expr.  Encode the STRING_CST
+   specified by EXPR into the buffer PTR of length LEN bytes.
+   Return the number of bytes placed in the buffer, or zero
+   upon failure.  */
+
+static int
+native_encode_string (const_tree expr, unsigned char *ptr, int len)
+{
+  tree type = TREE_TYPE (expr);
+  HOST_WIDE_INT total_bytes;
+
+  if (TREE_CODE (type) != ARRAY_TYPE
+      || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
+      || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
+      || !host_integerp (TYPE_SIZE_UNIT (type), 0))
+    return 0;
+  total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
+  if (total_bytes > len)
+    return 0;
+  if (TREE_STRING_LENGTH (expr) < total_bytes)
+    {
+      memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
+      memset (ptr + TREE_STRING_LENGTH (expr), 0,
+	      total_bytes - TREE_STRING_LENGTH (expr));
+    }
+  else
+    memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
+  return total_bytes;
+}
+
+
 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
    buffer PTR of length LEN bytes.  Return the number of bytes
@@ -7337,6 +7368,9 @@ native_encode_expr (const_tree expr, uns
     case VECTOR_CST:
       return native_encode_vector (expr, ptr, len);
 
+    case STRING_CST:
+      return native_encode_string (expr, ptr, len);
+
     default:
       return 0;
     }
--- gcc/testsuite/gfortran.dg/hollerith.f90.jj2	2008-09-30 16:56:06.000000000 +0200
+++ gcc/testsuite/gfortran.dg/hollerith.f90	2008-11-11 13:52:38.000000000 +0100
@@ -8,7 +8,7 @@ character z1(4)
 character*4 z2(2,2)
 character*80 line
 integer i
-logical l
+integer j
 real r
 character*8 c
 
@@ -20,15 +20,15 @@ data z2/4h(i7),'xxxx','xxxx','xxxx'/
 
 z2 (1,2) = 4h(i8)
 i = 4hHell
-l = 4Ho wo
+j = 4Ho wo
 r = 4Hrld! 
-write (line, '(3A4)') i, l, r
+write (line, '(3A4)') i, j, r
 if (line .ne. 'Hello world!') call abort
 i = 2Hab
 r = 2Hab
-l = 2Hab
+j = 2Hab
 c = 2Hab
-write (line, '(3A4, 8A)') i, l, r, c
+write (line, '(3A4, 8A)') i, j, r, c
 if (line .ne. 'ab  ab  ab  ab      ') call abort
 
 write(line, '(4A8, "!")' ) x
--- gcc/testsuite/gfortran.dg/transfer_simplify_4.f90	2008-09-30 16:56:06.000000000 +0200
+++ gcc/testsuite/gfortran.dg/transfer_simplify_4.f90	2008-09-30 10:18:57.740011359 +0200
@@ -1,30 +0,0 @@
-! { dg-do run }
-! { dg-options "-O0" }
-! Tests that the in-memory representation of a transferred variable
-! propagates properly.
-!
-  implicit none
-
-  integer, parameter :: ip1 = 42
-  logical, parameter :: ap1 = transfer(ip1, .true.)
-  integer, parameter :: ip2 = transfer(ap1, 0)
-
-  logical :: a
-  integer :: i
-  
-  i = transfer(transfer(ip1, .true.), 0)
-  if (i .ne. ip1) call abort ()
-
-  i = transfer(ap1, 0)
-  if (i .ne. ip1) call abort ()
-  
-  a = transfer(ip1, .true.)
-  i = transfer(a, 0)
-  if (i .ne. ip1) call abort ()
-
-  i = ip1
-  a = transfer(i, .true.)
-  i = transfer(a, 0)
-  if (i .ne. ip1) call abort ()
-
-end

	Jakub

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2008-11-12 17:42 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-11 22:53 [PATCH] Fold VIEW_CONVERT_EXPR <type, STRING_CST> generated by Fortran FE a lot (PR target/35366) Tobias Burnus
2008-11-12 14:20 ` Jakub Jelinek
2008-11-12 15:34   ` Paul Richard Thomas
2008-11-12 18:09   ` Feng Wang
  -- strict thread matches above, loose matches on Subject: below --
2008-11-11 13:27 Jakub Jelinek
2008-11-11 15:40 ` Richard Guenther
2008-11-11 15:53 ` Tobias Schlüter
2008-11-11 16:22   ` Jakub Jelinek
2008-11-11 16:26     ` Tobias Schlüter
2008-11-11 17:21       ` Jakub Jelinek
2008-11-11 17:22         ` Tobias Schlüter
2008-11-11 18:10           ` Jakub Jelinek
2008-11-11 19:07             ` Janne Blomqvist
2008-11-11 19:22             ` Brooks Moses
2008-11-11 19:36             ` Tobias Burnus
2008-11-11 20:50               ` Brooks Moses
2008-11-11 21:38                 ` Jakub Jelinek
2008-11-11 21:41                   ` Brooks Moses
2008-11-11 21:46                     ` Jakub Jelinek
2008-11-11 22:31                       ` Brooks Moses
2008-11-11 21:27             ` Thomas Koenig
2008-11-11 19:17       ` Brooks Moses
2008-11-11 19:34         ` Jakub Jelinek
2008-11-11 19:38           ` Brooks Moses
2008-11-11 17:30     ` Tobias Burnus
2008-11-11 16:22   ` Paul Richard Thomas
2008-11-11 16:45     ` Tobias Schlüter
2008-11-11 21:56   ` Steve Kargl

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