简体   繁体   中英

Can you do all gcc optimizations (-O2, -O3) manually in your c source code?

In my class project, my project is set to use gcc's optimization level of -O0 (no optimizations) and we are not allowed to change it for the final submission.

I tested my code using -O2 and got around a 2x speedup of my entire program. So I was wondering, is it possible to go through each optimization that -O2 does, and manually do those optimizations in my code? Or are some of the -O2 optimizations internal to the stack, frame, machine/assembly, etc, thus disallowing me, the programmer, from manually making those optimizations in my source code (If that makes sense)

Is it possible to go through each optimization that -O2 does, and manually do those optimizations in my code?

No. Many of the optimizations performed by the compiler cannot be represented in C. Some of these include:

  • Disabling the frame pointer
  • Removing unnecessary register saves/restores at the beginning and end of a function
  • "Peephole" optimizations on the assembly, such as removing redundant moves, loads, or stores
  • Inserting no-ops to align loops to specific address boundaries (typically 16 bytes)

This isn't to say that all of the optimizations performed by the compiler are untranslatable, of course -- merely that some of them are.

Yes, but that's the same as building your own 8086-class microprocessor in Minecraft — not worth your time and effort. And yes, many of those optimizations involve stuff below the language level of abstraction. Your professor might have unknown-to-you reasons for wanting an unoptimized executable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM