public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/30723]  New: Freeing memory doesn't need to call a library function
@ 2007-02-06 23:05 fxcoudert at gcc dot gnu dot org
  2007-02-06 23:06 ` [Bug fortran/30723] " fxcoudert at gcc dot gnu dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-02-06 23:05 UTC (permalink / raw)
  To: gcc-bugs

Currently, allocated memory is freed by calling the library function
_gfortran_internal_free, which does nothing but:

/* Free internally allocated memory.  Pointer is NULLified.  Also used to
   free user allocated memory.  */
void
internal_free (void *mem)
{ 
  if (mem != NULL)
    free (mem);
}

Two things are worth noting:
  + although the comment says otherwise, the pointer is not NULLified.
  + this code could be generated directly by the front-end, leading to better
optimization.

An example of the missed optimization is the compilation of the following code:

  REAL    :: a(5), b
  INTEGER :: l, u
  l = 4
  u = 2

  a = (/ 1.0, 2.0, 3.0, 4.0, 5.0 /)
  b = f(a(l:u) - 3.0)
  call foo(b)

  CONTAINS
    REAL FUNCTION f(x)
      REAL, DIMENSION(:), INTENT(in) :: x
      f = sum(x)
    end function
END PROGRAM

the optimized tree looks like:

  SR.33 = _gfortran_internal_malloc (0);
  b = 0.0; 
  _gfortran_internal_free (SR.33);

We sure could optimize away the use of SR.33. PR 30720 has another case where
such an optimization could happen.


-- 
           Summary: Freeing memory doesn't need to call a library function
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: fxcoudert at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30723


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

* [Bug fortran/30723] Freeing memory doesn't need to call a library function
  2007-02-06 23:05 [Bug fortran/30723] New: Freeing memory doesn't need to call a library function fxcoudert at gcc dot gnu dot org
@ 2007-02-06 23:06 ` fxcoudert at gcc dot gnu dot org
  2007-02-07  9:58 ` rguenth at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-02-06 23:06 UTC (permalink / raw)
  To: gcc-bugs



-- 

fxcoudert at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2007-02-06 23:05:58
               date|                            |
   Target Milestone|---                         |4.3.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30723


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

* [Bug fortran/30723] Freeing memory doesn't need to call a library function
  2007-02-06 23:05 [Bug fortran/30723] New: Freeing memory doesn't need to call a library function fxcoudert at gcc dot gnu dot org
  2007-02-06 23:06 ` [Bug fortran/30723] " fxcoudert at gcc dot gnu dot org
@ 2007-02-07  9:58 ` rguenth at gcc dot gnu dot org
  2007-02-07 19:22 ` fxcoudert at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-02-07  9:58 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2007-02-07 09:58 -------
Confirmed.  Note we already NULLify the pointer in the caller for
_gfortran_deallocate (but I missed to fix the comment before that function as
well).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30723


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

* [Bug fortran/30723] Freeing memory doesn't need to call a library function
  2007-02-06 23:05 [Bug fortran/30723] New: Freeing memory doesn't need to call a library function fxcoudert at gcc dot gnu dot org
  2007-02-06 23:06 ` [Bug fortran/30723] " fxcoudert at gcc dot gnu dot org
  2007-02-07  9:58 ` rguenth at gcc dot gnu dot org
@ 2007-02-07 19:22 ` fxcoudert at gcc dot gnu dot org
  2007-03-04 20:12 ` fxcoudert at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-02-07 19:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from fxcoudert at gcc dot gnu dot org  2007-02-07 19:22 -------
Created an attachment (id=13020)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=13020&action=view)
Patch to not generate calls to internal_free any more


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30723


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

* [Bug fortran/30723] Freeing memory doesn't need to call a library function
  2007-02-06 23:05 [Bug fortran/30723] New: Freeing memory doesn't need to call a library function fxcoudert at gcc dot gnu dot org
                   ` (2 preceding siblings ...)
  2007-02-07 19:22 ` fxcoudert at gcc dot gnu dot org
@ 2007-03-04 20:12 ` fxcoudert at gcc dot gnu dot org
  2007-03-04 21:02 ` rguenth at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-03-04 20:12 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from fxcoudert at gcc dot gnu dot org  2007-03-04 20:12 -------
Created an attachment (id=13141)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=13141&action=view)
Patch removing need for internal_malloc and internal_free library functions

Attached patch updates the previous one, and allows the front-end to generate
code to malloc and free directly. This removes the need for the library
internal_malloc and internal_free functions, and allows better optimization.


-- 

fxcoudert at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #13020|0                           |1
        is obsolete|                            |
         AssignedTo|unassigned at gcc dot gnu   |fxcoudert at gcc dot gnu dot
                   |dot org                     |org
             Status|NEW                         |ASSIGNED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30723


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

* [Bug fortran/30723] Freeing memory doesn't need to call a library function
  2007-02-06 23:05 [Bug fortran/30723] New: Freeing memory doesn't need to call a library function fxcoudert at gcc dot gnu dot org
                   ` (3 preceding siblings ...)
  2007-03-04 20:12 ` fxcoudert at gcc dot gnu dot org
@ 2007-03-04 21:02 ` rguenth at gcc dot gnu dot org
  2007-05-14 19:34 ` fxcoudert at gcc dot gnu dot org
  2007-05-14 19:35 ` fxcoudert at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-03-04 21:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from rguenth at gcc dot gnu dot org  2007-03-04 21:01 -------
Note that we currently do not optimize

 tmp = malloc (XXX);
 free (tmp);

That would require some special handling in DCE (or a completely separate
pass).


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu dot
                   |                            |org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30723


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

* [Bug fortran/30723] Freeing memory doesn't need to call a library function
  2007-02-06 23:05 [Bug fortran/30723] New: Freeing memory doesn't need to call a library function fxcoudert at gcc dot gnu dot org
                   ` (4 preceding siblings ...)
  2007-03-04 21:02 ` rguenth at gcc dot gnu dot org
@ 2007-05-14 19:34 ` fxcoudert at gcc dot gnu dot org
  2007-05-14 19:35 ` fxcoudert at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-05-14 19:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from fxcoudert at gcc dot gnu dot org  2007-05-14 20:34 -------
Subject: Bug 30723

Author: fxcoudert
Date: Mon May 14 19:33:57 2007
New Revision: 124721

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=124721
Log:
       PR fortran/30723

        * trans.h (gfor_fndecl_internal_malloc, gfor_fndecl_internal_malloc64,
        gfor_fndecl_internal_free): Remove prototypes.
        (gfor_fndecl_os_error, gfc_call_free, gfc_call_malloc): Add prototypes.
        * trans.c (gfc_call_malloc, gfc_call_free): New functions.
        * f95-lang.c (gfc_init_builtin_functions): Add __builtin_free
        and __builtin_malloc builtins.
        * trans-decl.c (gfor_fndecl_internal_malloc,
        gfor_fndecl_internal_malloc64, gfor_fndecl_internal_free): Remove.
        (gfor_fndecl_os_error): Add.
        (gfc_build_builtin_function_decls): Don't create internal_malloc,
        internal_malloc64 and internal_free library function declaration.
        Create os_error library call function declaration.
        * trans-array.c (gfc_trans_allocate_array_storage,
        gfc_trans_auto_array_allocation, gfc_trans_dummy_array_bias,
        gfc_conv_array_parameter, gfc_duplicate_allocatable): Use
        gfc_call_malloc and gfc_call_free instead of building calls to
        internal_malloc and internal_free.
        * trans-expr.c (gfc_conv_string_tmp): Likewise.
        * trans-stmt.c (gfc_do_allocate, gfc_trans_assign_need_temp,
        gfc_trans_pointer_assign_need_temp, gfc_trans_forall_1,
        gfc_trans_where_2: Likewise.
        * trans-intrinsic.c (gfc_conv_intrinsic_ctime,
        gfc_conv_intrinsic_fdate, gfc_conv_intrinsic_ttynam,
        gfc_conv_intrinsic_array_transfer, gfc_conv_intrinsic_trim): Likewise.

        * runtime/memory.c (internal_malloc, internal_malloc64,
        internal_free): Remove.
        * runtime/error.c (os_error): Export function.
        * intrinsics/move_alloc.c: Include stdlib.h.
        (move_alloc): Call free instead of internal_free.
        (move_alloc_c): Wrap long lines.
        * libgfortran.h (os_error): Export prototype.
        (internal_free): Remove prototype.
        * gfortran.map (GFORTRAN_1.0): Remove _gfortran_internal_free,
        _gfortran_internal_malloc and _gfortran_internal_malloc64.
        Add _gfortran_os_error.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/f95-lang.c
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-intrinsic.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/fortran/trans.c
    trunk/gcc/fortran/trans.h
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/gfortran.map
    trunk/libgfortran/intrinsics/move_alloc.c
    trunk/libgfortran/libgfortran.h
    trunk/libgfortran/runtime/error.c
    trunk/libgfortran/runtime/memory.c


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30723


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

* [Bug fortran/30723] Freeing memory doesn't need to call a library function
  2007-02-06 23:05 [Bug fortran/30723] New: Freeing memory doesn't need to call a library function fxcoudert at gcc dot gnu dot org
                   ` (5 preceding siblings ...)
  2007-05-14 19:34 ` fxcoudert at gcc dot gnu dot org
@ 2007-05-14 19:35 ` fxcoudert at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-05-14 19:35 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from fxcoudert at gcc dot gnu dot org  2007-05-14 20:34 -------
Fixed on mainline.


-- 

fxcoudert at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30723


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

end of thread, other threads:[~2007-05-14 19:35 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-06 23:05 [Bug fortran/30723] New: Freeing memory doesn't need to call a library function fxcoudert at gcc dot gnu dot org
2007-02-06 23:06 ` [Bug fortran/30723] " fxcoudert at gcc dot gnu dot org
2007-02-07  9:58 ` rguenth at gcc dot gnu dot org
2007-02-07 19:22 ` fxcoudert at gcc dot gnu dot org
2007-03-04 20:12 ` fxcoudert at gcc dot gnu dot org
2007-03-04 21:02 ` rguenth at gcc dot gnu dot org
2007-05-14 19:34 ` fxcoudert at gcc dot gnu dot org
2007-05-14 19:35 ` fxcoudert at gcc dot gnu dot org

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