Redundant build instructions?

I recently tried to learn reverse engineering. So I delved into a lot of build code. What puzzles me is the following:

movq %rax,0xf8(%rbp) movq 0xf8(%rbp),%rax 

I have seen this several times. Isn't that redundant? Why does the compiler do this? The binary I am viewing was compiled with gcc.

+4
source share
1 answer

You probably compiled without optimization (-O). What you see is a direct, naive translation of an intermediate view. Such fragments are usually related to the fact that the value is stored in a local variable, in this case 0xf8 (% rbp). Then the value is used immediately after that, so it loads it into the% rax register again. The optimizer will find that saving from% rax only to restore back to the same register is redundant and completely deletes the sequence. If all stages of optimization do not work, at least the eye will determine these two instructions in a row.

If you really turned on optimization, this is really strange, but can be explained if you place a large (but not too large) sequence. There are still many cases where something clearly suboptimal will be generated, but nothing is so contradictory.

+9
source

Source: https://habr.com/ru/post/1340740/


All Articles