This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

optimization/6854: missed optimization with attribute const & pure

>Number:         6854
>Category:       optimization
>Synopsis:       missed optimization with attribute const & pure
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          pessimizes-code
>Submitter-Id:   net
>Arrival-Date:   Tue May 28 18:56:00 PDT 2002
>Originator:     Kaveh Ghazi
>Release:        gcc version 3.2 20020527 (experimental)
Probably all, but known to occur on at least:
and judging by the testresults:
it also occurs on i686, ia64 and alpha.
Using the current 3.2 trunk, gcc fails to optimize several sections in the testcase attached.  It's also installed as:

The testcase is designed to emit link errors when the optimizations fail to occur.  When I compile it (e.g. with -O/-O2) I get:

Undefined                       first referenced
 symbol                             in file
link_error4                         /var/tmp//cci2nHoc.o
link_error5                         /var/tmp//cci2nHoc.o
link_error6                         /var/tmp//cci2nHoc.o
link_error7                         /var/tmp//cci2nHoc.o

As noted by the particular optimizing failures, this appears to relate to the inability of gcc to detect as pure or const a function which contain calls to other const/pure functions.

NOTE: gcc-3.1.1-pre and 3.0.3 fail on 2, 4 and 6 so the appearance of link_error 5 & 7 represents a regression.  While the disappearance of 2 is a (slight) improvement.


Compile the attached testcase with -O

Content-Type: text/plain; name="pure-1.c"
Content-Disposition: inline; filename="pure-1.c"

/* Origin: Kaveh Ghazi <> 2002-05-27.  */

/* Use a different function for each test so the link failures
   indicate which one is broken.  */
extern void link_error0 (void);
extern void link_error1 (void);
extern void link_error2 (void);
extern void link_error3 (void);
extern void link_error4 (void);
extern void link_error5 (void);
extern void link_error6 (void);
extern void link_error7 (void);

extern int i;

extern int func0 (int) __attribute__ ((__pure__));
extern int func1 (int) __attribute__ ((__const__));
/* GCC should automatically detect attributes for these functions.
   Don't allow -O3 to inline them.  */
#define ANI __attribute__ ((__noinline__))
static int ANI func2 (int a) { return i + a; } /* pure */
static int ANI func3 (int a) { return a * 3; } /* const */
static int ANI func4 (int a) { return func0(a) + a; } /* pure */
static int ANI func5 (int a) { return a + func1(a); } /* const */
static int ANI func6 (int a) { return func2(a) + a; } /* pure */
static int ANI func7 (int a) { return a + func3(a); } /* const */

int main ()
  int i[10], r;

  i[0] = 0;
  r = func0(0);
  if (i[0])

  i[1] = 0;
  r = func1(0);
  if (i[1])

  i[2] = 0;
  r = func2(0);
  if (i[2])

  i[3] = 0;
  r = func3(0);
  if (i[3])

  i[4] = 0;
  r = func4(0);
  if (i[4])

  i[5] = 0;
  r = func5(0);
  if (i[5])

  i[6] = 0;
  r = func6(0);
  if (i[6])

  i[7] = 0;
  r = func7(0);
  if (i[7])

  return r;

int func0 (int a) { return a - i; } /* pure */
int func1 (int a) { return a - a; } /* const */

int i = 2;

#ifndef __OPTIMIZE__
/* Avoid link failures when not optimizing. */
void link_error0() {}
void link_error1() {}
void link_error2() {}
void link_error3() {}
void link_error4() {}
void link_error5() {}
void link_error6() {}
void link_error7() {}
#endif /* ! __OPTIMIZE__ */

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]