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

Re: alignment


In article <006401bfee63$0d4ff4a0$0100000a@TIMYX18EWDT6RQ>,
Tim Prince <tprince@computer.org> wrote:
>Did you do any performance studies?  Did you enable the use of .p2align,
>and did that change the situation?  On the code I have worked with, I
>saw no reason for using __volatile__ other than to hide bugs, and that
>may make a difference.

The gcc manual explicitly states:

	Note that if there are only unused output operands, you will
	then also need to specify @code{volatile} for the @code{asm}
	construct, as described below. 

	...

	If the side-effects of your instruction are not purely external,
	but will affect variables in your program in ways other than
	reading the inputs and clobbering the specified registers or
	memory, you should write the @code{volatile} keyword to prevent
	future versions of GNU CC from moving the instruction around
	within a core region. 

	...

For example, a common thing to do is to encode the "inb" instruction on
x86, which gcc has absolutely no way of getting at any other way than
through the use of inline asm. 

The natural encoding for "inb" would be:

	static inline unsigned char inb(unsigned short port)
	{
		unsigned char byte;
		asm("inb":"=a" (byte):"Nd" (port));
		return byte;
	}

would you not agree?

However, the above is WRONG. It is wrong because:

 - it can get re-ordered

 - it can get deleted if the value is not actually used

both of which are very very wrong, as the "inb" instruction obviously
has tons of side effects that basically cannot be described to the
compiler. 

Pray tell me how you'd fix that without the use of "volatile"?

[ Yes, you _can_ fix it. You can lie. You can say that the instruction
  changes memory, and use a "memory" clobber. That should make gcc
  unable to re-order it or remove it, because we're saying that it
  changes memory in ways that gcc doesn't know about. Is that really the
  preferred syntax? And if so, what's the point in having a __volatile__
  at all? Makes no sense to me: the __volatile__ thing at least is
  conceptually understandable, a "memory" clobber would just be an
  obvious workaround for a lack in the compiler ]

In short, your statement that "volatile" is mainly to be used to hide
bugs is nonsense, and it's obviously equally non-sensical for gcc to
insert a ".align" directive in front of the asm.

If the user is using inline assembly and really wants to align the code,
he can damn well do so by hand.  The compiler doing it for him is
obviously an extremely silly thing to do that buys absolutely nothing.

			Linus

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