Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can two instructions execute in the same cycle if the same register is used as input and output respectively?

For example, could these two instructions begin executing in the same cycle or do they interfere with each other?

MOV  %RAX, (ADDR)      # AT&T syntax: store rax
POP  %RAX
like image 730
Eloff Avatar asked Jan 06 '23 11:01

Eloff


1 Answers

Yes, they can execute in the same cycle on a modern out-of-order CPU, because they use Tomasulo's algorithm for register renaming. It totally avoids all write-after-read hazards like this, and also write-after-write, so out-of-order execution is only limited by Read-after-Write true dependencies.

After register renaming, pop will write the new value of the RAX architectural register to a different physical register than the one holding the old value of RAX (which as the input for the store).

The instructions have zero interaction with each other, because the pop starts a new dependency chain for the value of RAX. (Its output is write-only.) The pop (and following instructions that read/write RAX) could be executed many cycles before the store, especially if the value to be stored is produced by a long dependency chain.

See also Agner Fog's microarch pdf and other links in the x86 tag wiki.


update: I thought you were asking about two instructions that write the same register or read the same register, since your original example code left off the % marks, and used AX, so I assume it was 16-bit Intel-syntax. Leaving this part in as a bonus anyway.

Two instructions reading the same register (read-after-read) is not even a hazard at all.

Intel CPUs before Sandybridge have a limitation on the number of not-recently-modified registers that can be read in the same cycle, but reading the same register multiple times in nearby instructions is not a problem. (Search for register read port stalls in Agner Fog's microarch pdf, and/or see this answer where it was relevant).

like image 127
Peter Cordes Avatar answered Jan 13 '23 11:01

Peter Cordes