Re: Attribute const and inline functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2014-02-24 10:57, David Brown wrote:
On 22/02/14 19:28, Sebastian Huber wrote:
Hello,

first some background information.  I would like to read from a memory
mapped register (some sort of hardware counter).  The problem is that
the address of this register is unknown at compile and link time.  So
some low-level initialization code determines the register address and
later its value never changes.

I hoped that the const function attribute is of some help here:

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function-Attributes


Here it explicitly says "function is not allowed to read global
memory".  Unfortunately the compiler takes this serious.

I have the following test code:

extern volatile const int *ip;

__attribute__((const)) volatile int *get_ip(void);

/* This function reads from global memory and is declared as const. GCC
ignores the const attribute in this case. */
__attribute__((const)) static inline volatile int *get_ip_inline(void)
{
     return ip;
}

Try the "pure" attribute rather than the "const" one.  "pure" functions
are allowed to read global memory, but not write it (or cause other
side-effects), and the compiler can safely call them fewer times than
the program says.

I noticed that even if you have a const variable, e.g.

extern const int const_var;

GCC will read the value of const_var again after a compiler memory barrier (e.g. __asm__ volatile("" ::: "memory")). So in my case it turned out that there is not much left to optimize. It seems that I have to live with the superfluous loads.


Also, you don't say anything about optimisation options - "pure" and
"const" attributes only help if optimisation is enabled.


I use optimization level -O2.

--
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax     : +49 89 189 47 41-09
E-Mail  : sebastian.huber@xxxxxxxxxxxxxxxxxx
PGP     : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux