0

When I disassemble some C++ code, sometimes I see assembler doing like this:

movq   %rdi, -0x8(%rbp)
movq   -0x8(%rbp), %rdi

Isn't the second instruction useless? Why is it here?

Compiled with clang8 in debug mode with no specified optimization level. It only remains with -O0 level, with -O1, -O2, -O3 it disappears.

sham42
  • 1
  • 1
  • 1
  • Did you compile with optimization enabled? – Eric Postpischil Mar 04 '20 at 01:12
  • 2
    This looks like a classic case of debug-mode code-gen, spilling a function arg and then reloading it for the first statement (that passes it to another function? Normally compilers prefer EAX/RAX) TL:DR of my answer on the linked duplicate: debug mode doesn't optimize across C++ statements so spill/reload is normal. – Peter Cordes Mar 04 '20 at 01:14
  • You didn't provide how you compiled the code. `-O0`? `-O3`? (Or the MSVC++ equivalent. Or whatever compiler you are using.) – Eljay Mar 04 '20 at 01:42
  • 1
    Congratulations! You discovered that when not asked for careful code writing (optimization), compilers often write stereotypical, dumb code. (Doing otherwise takes time, and you asked not to care). – vonbrand Mar 04 '20 at 12:03
  • `-O0` is the default. – Peter Cordes Mar 04 '20 at 15:04

0 Answers0