A non-terminating program can do useful work as it is running, and replacing it with a single instruction loop would not be a correct compilation.
A programmer could choose a lower level of optimization for reasons other than compile time, e.g., for debugging. -- Shmuel (Seymour J.) Metz http://mason.gmu.edu/~smetz3 ________________________________________ From: IBM Mainframe Assembler List [[email protected]] on behalf of Martin Ward [[email protected]] Sent: Saturday, November 13, 2021 2:57 PM To: [email protected] Subject: Re: Curious compiler optimization 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://secure-web.cisco.com/1RyY9zGJfTPFPZnnZTcrDZOf1JaEXKfGzKVtcCQBz9aB9hNogfdv8CyLNuJzmu8-6pTQ6z0Cwsr48kkCRfcI_4bHbfhRjBkGENJSDeEyDxP7VXLeBw4rYC5VqSu6VnMaB8R6FubtJxLCjLFxNJpWbTm_3xhacCmyT2K2mPw4n4vsvD_4RCq5CO3OeYM-VqsbqsEG4PZj8-ilzkMFVEoztKY4UjvuIjP_RVXPMxAtGImPI8QmOhqZglBsTFtYssrqUF8gKc4CTbMal7THPtydCudSKhVr4mfrMRL7rhrSwVWLklZXHjib1e6e0JgaKYQvReoj-acnxx3AfDwijsgIhXrPUwEgpacL6qXrIYGQidHBz30tVLCrsZ_JQ-upvTrIfzqoxZb19sEFGlBJgApfDx9lbiqgF5dJ4PkVpnnjhujf3EOjHmO9IVg717vJCmQ3h/http%3A%2F%2Fwww.gkc.org.uk G.K.Chesterton site: http://secure-web.cisco.com/18rreaVxNWCDz7b6ngoJqN0uBZmS39-CHKrY3re8oTNPCgOQvmqx-c9TCCOWn21E_62EIhmH7gYiRCkUxd2lUpSC71JNZinAe98CWE66vvZQeTLUE0DNPG9lvBrbDm5gQhmS_WBa6d4cz2H14Yktdjs2X6to-_yF4d_YlzQUg1EaEyH4X0j4KjWcLsykLQZcvkkxunID26yFeSVyTR3-x7GO_KwiFk1JKhxbXO8IAVpjwtyUbKHNedpdNWuYBOTNeeJy_FSSS3_Iw4gSipMFcaApsqqr9ihFGKCIbnd3euFJ4Ag2Hnxd9YELW4Hg3I8m_JYysX5F587FA8p9QUa-uiybpwDSMXkOPE8ggFbuUr51hO_l_0nmCOy2CRVvot_ysFxgCiKq1v1qqm8-489LDoeGsCELiVJ6HtVxJTyIIGdTwmWwN8jet6D8TJioRw5QG/http%3A%2F%2Fwww.gkc.org.uk%2Fgkc | Erdos number: 4
