43

I am trying to optimize a lot of multiplications and pointer arithmetics and would like to see what the compiler does underneath when I put in optimization flags.

--Edit--

How to restrict it to a specific function or a code block?

--Edit_2--

How to let gcc generate a less verbose assembly-code?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
  • 2
    A less verbose assembly? What exactly do you mean by that? –  Aug 30 '09 at 21:29
  • an assembly-code. In other words I dont want to extrenious things such as initializations of activation records, but only the code relavent to my statemetns. hope it makes it clearer. – vehomzzz Aug 30 '09 at 21:31
  • 1
    so you want gcc to generate _incorrect_ assembly code, that only satisfies your sence of beauty? No, sir. – P Shved Aug 30 '09 at 21:39
  • 1
    LOL I want to generate a correct one, but to display it a concise one for me. Is that too much to ask? No big deal though, extra info won't kill me. I was just wondering if it possible at all, or there are tools available..... Pavel, I don't think you understand my question entirely – vehomzzz Aug 30 '09 at 22:17
  • 4
    It is a bit much to ask, yes. The assembly it generates is precisely the code that the CPU will execute. If you need it to only show you the code that is relevant to you, then write a simplified function that does that operation, compile it, and then view the assembly. – greyfade Aug 30 '09 at 22:46
  • 3
    And of course, the whole notion of "the code for a specific function" doesn't exist anymore once an optimizer inlined, reordered, merged and/or eliminated assembly instructions. – MSalters Aug 31 '09 at 08:09
  • Have you played around with https://godbolt.org/ ? You should check it out. – Zan Lynx Feb 27 '18 at 20:19

4 Answers4

42

Add -S switch to your command line.

Edit: Do not forget that it will place the assembly to the files you specified under -o switch.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
P Shved
  • 96,026
  • 17
  • 121
  • 165
  • 2
    You can use `gcc -O3 -march=native foo.c -S -o- | less` to pipe into `less` instead of creating a `.s` file. See also [How to remove “noise” from GCC/clang assembly output?](https://stackoverflow.com/questions/38552116/how-to-remove-noise-from-gcc-clang-assembly-output) for more tips about seeing the "interesting part" of the asm output, especially **Matt Godbolt's CppCon2017 talk: [“What Has My Compiler Done for Me Lately? Unbolting the Compiler's Lid”](https://youtu.be/bSkpMdDe4g4)** – Peter Cordes Feb 27 '18 at 20:00
9

How to restrict it to a specific function or a code block?

Put that function in a separate source file (and use a different command-line parameter for that one source file).

ChrisW
  • 54,973
  • 13
  • 116
  • 224
  • 1
    That's what I have been doing. I am curious if it's possible from just options. – vehomzzz Aug 30 '09 at 21:24
  • 1
    This may not reflect how it's really optimized if it can inline into callers. (Especially with link-time optimization). Having even one parameter be a compile-time constant can make a big difference, or having the alignment or size of an array known can change auto-vectorization a lot. But yes, this is good *if* you understand what you're doing. – Peter Cordes Feb 27 '18 at 20:04
5

You could also run that program in a debugger like gdb and use a disassembly view. In gdb you could use the command disass/m to view the assembly mixed with the C code on the current location.

Chris
  • 3,265
  • 5
  • 37
  • 50
3

You could stop you program at a breakpoint in the Visual Studio debugger and do "show assembly" and even step through it one instruction at a time.

sean riley
  • 2,633
  • 1
  • 22
  • 22