asm volatile statement reordering
Xi Ruoyao
ryxi@stu.xidian.edu.cn
Tue Oct 17 04:20:00 GMT 2017
On 2017-10-17 00:27 +0200, David Brown wrote:
> My testing suggests that gcc will re-order "asm volatile" statementsÂ
> that have an output, such as the "save the PRIMASK into status"Â
> statement, but it will /not/ re-order "asm volatile" statements thatÂ
> have no outputs.
>
> Is that correct?
>
> Is that the intended behaviour of "asm volatile" ?
Yes. It's said in the documentation
<https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Extended-Asm.html>:
Note that the compiler can move even volatile asm instructions relative
to other code, including across jump instructions. For example, on many
targets there is a system register that controls the rounding mode of
floating-point operations. Setting it with a volatile asm, as in the
following PowerPC example, does not work reliably.
asm volatile("mtfsf 255, %0" : : "f" (fpenv));
sum = x + y;
The compiler may move the addition back before the volatile asm. To
make it work as expected, add an artificial dependency to the asm by
referencing a variable in the subsequent code, for example:
asm volatile ("mtfsf 255,%1" : "=X" (sum) : "f" (fpenv));
sum = x + y;
> If so, is that a good design choice or should it be changed?
I don't know. I think it is just a choice, not so good and not so
bad.
> Could the documentation in the gcc web page be improved?
The documentation is very clear.
> Or is this a bug in gcc, and the statements should not have been re-ordered?
>
>
> David
--
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University
More information about the Gcc-help
mailing list