On 13/11/2021 16:10, Phil Smith III wrote:
Corollary: the compiler generates...deliberately poor code without
optimization? Again, why?

Every compiler, even the world's best optimising compiler
with all optimisations enabled, will generate poor code
(less than optimal code) in some cases.

This is the "Full Employment Theorem for Compiler Writers":
there will always be work for compiler writers to do.
It is a consequence of the Halting Problem: a perfect
optimising compiler would optimise all non-terminating
programs to a single instruction infinite loop.
So a perfect optimising compiler can solve the Halting Problem:
which is impossible. So a perfect optimising compiler does not exist.

Therefore any existing compiler will not be perfect and will
miss some possible optimisations.

But why does the un-optimised code not include the optimisations
that the optimised code has? Well, compiling is always
a trade-off between compile time and execution time.
By turning off optimisation the programmer has declared
that compile time is the most important factor: so the compiler
will parse the source and generate some kind of working code
as quickly as possible, without worrying about the execution
time of the generated code. If you want better code,
and you are prepared to spend more time waiting for it to
be produced, then simply turn on optimisation!

--
                        Martin

Dr Martin Ward | Email: [email protected] | http://www.gkc.org.uk
G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4

Reply via email to