r/Compilers • u/BrainThinkerMan • Oct 17 '26
code optimization
ive seen that compiler middle end optimized code,
how do i optimize that code without the compiler; and then the
compiler takes less time doing it for me.
ie; i change some loops to be better, and change ome if statments to add less conditions
0
Upvotes
View all comments
2
u/WittyStick Oct 17 '26 edited Oct 17 '26
You don't really. The compiler is going to do all of those optimization passes regardless of whether your code is structured the right way to begin with, so it won't shorten compile times.
You can disable specific optimization passes through compiler flags though, but it's unlikely to make any noticeable difference to compilation time.
Usually code is compiled with
-O2, which isn't a specific optimization but just enables a bunch of optimizations, which can be seen here for example with GCC.Each of these settings, such as
-fmove-loop-invariantscan be disabled with a matching option-fno-move-loop-invariants, so we can have-O2 -fno-move-loop-invariantsto have all the other-O2optimizations except that one.You can also enable or disable specific optimizations for certain regions of code rather than globally through the command line, using
#pragma GCC optimize (...)or__attribute__((optimize(...))).