question on optimizations: when to stop??

hi,

i have some LLVM bitcode, and trying to write a simple LLVM-based code
to optimize them. i simply apply some selected passes from
http://llvm.org/docs/Passes.html

if i run the these optimizes once on the input LLVM bitcode, the
output bitcode is optimized.
but to my surprise, if i run the optimizes once more, the output
bitcode is further optimized.

so i have few questions:

1. why running optimizes once is not enough? they are not smart enough
to figure out that they should repeat the optimizes once more, until
no optimized can be done anymore?

2. in case that optimizes must be repeated in multiple rounds, how can
i know when is the time to stop? (ie when i dont need to run the
output bitcode thru the optimizes anymore?)

thanks so much!
Jun

any help, please?

thanks!
Jun

Jun Koi <junkoi2004@gmail.com> writes:

i have some LLVM bitcode, and trying to write a simple LLVM-based code
to optimize them. i simply apply some selected passes from
http://llvm.org/docs/Passes.html

if i run the these optimizes once on the input LLVM bitcode, the
output bitcode is optimized.
but to my surprise, if i run the optimizes once more, the output
bitcode is further optimized.

so i have few questions:

1. why running optimizes once is not enough? they are not smart enough
to figure out that they should repeat the optimizes once more, until
no optimized can be done anymore?

Some individual optimization passes are not smart enough (actually, they
don't invest effort on) noticing that their own output can benefit for
re-running themselves over their own output. This is more noticeable
with series of optimizations, though. An optimization pass transforms
the code, probably creating new opportunities for other passes. For
instance, if you have a series of passes A,B,C, after running C the code
may be transformed on a way that re-running A and/or B again can bring
further improvements.

2. in case that optimizes must be repeated in multiple rounds, how can
i know when is the time to stop? (ie when i dont need to run the
output bitcode thru the optimizes anymore?)

Well, the definitive answer is to experiment by trial and error for a
specific initial code. You can apply some intuition when you know well
what every optimization does. But in the end, you need to reach a
compromise consisting on a series of passes that optimize enough, run
fast enough and are generic enough to work reasonably well on average
with the programs you wish to optimize. This is what the compiler
designers did when they decided which optimization series corresponds to
the standard optimization levels (-O1, -O2 etc)

Your questions are basic ones and not LLVM-specific. Learning a bit of
theory about optimization and/or passing different types of code through
the LLVM optimizer (`opt') one pass at the time and examining the result
of each pass will give you more insight on the matter than any
explanation on this mailing list.