Bug 20641 - Missed optimization on the tree level (malloc attribute)
Summary: Missed optimization on the tree level (malloc attribute)
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.1.0
: P2 enhancement
Target Milestone: 4.1.0
Assignee: Not yet assigned to anyone
URL:
Keywords: alias, missed-optimization, TREE
Depends on:
Blocks: 20638
  Show dependency treegraph
 
Reported: 2005-03-25 20:59 UTC by Andrew Pinski
Modified: 2016-11-17 22:01 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-06-09 16:36:26


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2005-03-25 20:59:21 UTC
Give the following code, we don't optimize it on the tree level:
typedef __SIZE_TYPE__ size_t;
extern void link_error();
extern void *f (size_t) __attribute__((__malloc__));
static int x;

void test3(void)
{
  int *ptr1, *ptr2;

  ptr1 = &x;
  ptr2 = (int*) f(sizeof (int));

  *ptr1 = 12;
  *ptr2 = 8;

  if (*ptr1 != 12)
    link_error();  //<-- This should be gone
}
Comment 1 Andrew Pinski 2005-09-21 02:12:44 UTC
Fixed for 4.1.0.
Comment 2 Martin Sebor 2016-11-17 21:56:12 UTC
The test case in comment #0 is optimized as expected but not because of attribute malloc but rather because x is static and function f is not defined in the same translation unit, meaning it can't access x.  The test case is optimized on this basis even without attribute malloc:

$ cat x.c && gcc -O2 -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout x.c
extern int* f (void);

static int x;

void g (void)
{
  int *p = &x;
  int *q = f ();

  *p = 12;
  *q = 8;

  if (*p != 12)  __builtin_abort ();
}

;; Function g (g, funcdef_no=0, decl_uid=1798, cgraph_uid=0, symbol_order=1)

g ()
{
  int * q;

  <bb 2>:
  q_4 = f ();
  x = 12;
  *q_4 = 8;
  return;

}
Comment 3 Martin Sebor 2016-11-17 22:01:02 UTC
To be clear, a test case that shows the effect of attribute malloc is one where the variable is extern, like below.  (For the record, I got here by trying to devise a test case for the attribute and having  trouble with it as noted in bug 78412).

$ cat x.c && gcc -O2 -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout x.c
extern int* f0 (void);
extern int* __attribute__ ((malloc)) f1 (void);

extern int x;

void g0 (void)
{
  int *p = &x;
  int *q = f0 ();

  *p = 12;
  *q = 8;

  if (*p != 12)  __builtin_abort ();
}

void g1 (void)
{
  int *p = &x;
  int *q = f1 ();

  *p = 12;
  *q = 8;

  if (*p != 12)  __builtin_abort ();
}


;; Function g0 (g0, funcdef_no=0, decl_uid=1800, cgraph_uid=0, symbol_order=0)

g0 ()
{
  int * q;
  int _1;

  <bb 2>:
  q_4 = f0 ();
  x = 12;
  *q_4 = 8;
  _1 = x;
  if (_1 != 12)
    goto <bb 3>;
  else
    goto <bb 4>;

  <bb 3>:
  __builtin_abort ();

  <bb 4>:
  return;

}



;; Function g1 (g1, funcdef_no=1, decl_uid=1805, cgraph_uid=1, symbol_order=1)

g1 ()
{
  <bb 2>:
  f1 ();
  x = 12;
  return;

}