This is the mail archive of the gcc-help@gcc.gnu.org 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]

Variable access optimization


Hi All,

It seems gcc (may be from 5) optimizes the global variable's accesses though standard C functions are called in-between the accesses.

Example:
  1 #include <stdlib.h>
  2
  3 struct __freelist {
  4     size_t sz;
  5     struct __freelist *nx;
  6 };
  7
  8 extern char *__brkval;
  9 extern struct __freelist *__flp;
 10 int main(void)
 11 {
 12     char *p, *p1;
 13
 14     p = malloc(16);
 15     if (!p)
 16         return 1;
 17
 18     /* releasing 6 bytes creates a new free chunk */
 19     p1 = realloc(p, 10);
 20     if (p != p1)
 21         return 2;
 22
 23     p = p1 + 10;
 24     if (__brkval != p)
 25         return 3;
 26
 27     /* use the last free chunk */
 28     p1 = malloc(4);
 29     if (!p1)
 30         return 4;
 31
 32     p = malloc(10);
 33     if (!p)
 34         return 6;
 35
 36     /* force creation of a new minimal free chunk (sz = 3), which
 37            will be returned from heap then */
 38     p1 = realloc(p, 5);
 39     if (p != p1)
 40         return 7;
 41
 42     p = p1 + 5;
 43     if (__brkval != p)
 44         return 8;
 45
 46     return 0;
 47 }

This example is derived from avr-libc's test.
Ref: http://svn.savannah.nongnu.org/viewvc/trunk/avr-libc/tests/simulate/regression/bug-27242.c?revision=2131&root=avr-libc&view=markup

avr-gcc (used gcc-5.2) optimizes the __brkval access at line 43 by reusing the registers which is loaded with __brkval value. Is that expected?

When -ffreestanding option is enabled, the global variable is loaded from memory for line #43. 
Or is there any specific option to avoid optimizing such variable access?

Regards,
Pitchumani


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