------- Comment #18 from rguenth at gcc dot gnu dot org  2010-04-28 14:52 
-------
Updating the status on this bugreport.  I am working on middle-end support
for hoisting/sinking malloc/free pairs out of loops (in case the size is
loop invariant).  The Fortran FE makes this somewhat difficult.

For

subroutine test0(esss,Ix)
  integer(kind=kind(1)), dimension(:), pointer :: esss
  integer(kind=kind(1)), dimension(:), pointer :: Ix
  esss = Ix + Ix
end subroutine

It creates

    D.1552 = ix->dim[0].lbound;
    D.1553 = ix->dim[0].ubound;
...
    D.1562 = D.1553 - D.1552;
...
    D.1571 = D.1562 < 0;
    D.1572 = D.1562 + 1;
    D.1573 = D.1571 ? 0 : D.1572 * 4;
    D.1574 = (void * restrict) __builtin_malloc (MAX_EXPR <D.1573, 1>);
    D.1575 = D.1574;
    atmp.0.data = D.1575;
...
    {
      ...
      S.1 = 0;
      while (1)
        {
          if (S.1 > D.1562) goto L.1;
...
        }
      L.1:;
      S.1 = 0;
      while (1)
        {
          if (S.1 > D.1562) goto L.2;
...
        }
      L.2:;
    }
    {
      void * D.1576;

      D.1576 = (void *) atmp.0.data;
      if (D.1576 != 0B)
        {
          __builtin_free (D.1576);
        }
    }


two unfortunate facts remain.

  1) __builtin_free is executed conditionally even though free(0) is
well-defined
  2) __builtin_malloc (MAX_EXPR <D.1571 ? 0 : D.1572 * 4, 1>) later causes DOM
to jump-thread this into two malloc calls, one malloc(1) and one malloc(D.1573)

especially the latter is very hard to get rid of at the tree level later
(the conditional free can possibly be made unconditional by optimizers).

Thus I would request that for the purpose of allocating array temporaries
from the scalarizer you

 1) unconditionally free the memory
 2) drop the MAX_EXPR, malloc (0) is well-defined and we can just fold that to
NULL if DOM still thinks to jump-thread that
 3) for the same reason you can also drop the + 1 in computing the allocation
size which is currently (ubound - lbound + 1) * 4

even better would be to guard the allocation by the loop entry check
(thus ubound - lbound >= 0)

If that's all acceptable I will work on this soon.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |rguenth at gcc dot gnu dot
                   |dot org                     |org
             Status|NEW                         |ASSIGNED
   Last reconfirmed|2010-03-27 18:55:47         |2010-04-28 14:52:15
               date|                            |


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

Reply via email to