4 ms·
Generating optimal code is much like the travelling salesman problem. Optimization algorithms will lead the result to a local minima, while missing a global min
by WalterBright 12y ago
Generating optimal code is much like the travelling salesman problem. Optimization algorithms will lead the result to a local minima, while missing a global minima.
- Ono-Sendai 12y agoIf the function is defined on a finite domain, you can (in theory) find the fastest algorithm for it: http://www.forwardscattering.org/post/13 http://www.forwardscattering.org/post/13 In practice, something like hill climbing will be done, as you said. Actually it's not even hill climbing, since some of the changes will be pessimisations. (as per the original post)
- WalterBright 12y agoMany optimizations in isolation are pessimizations, they only produce net improvement when used in combination with other optimizations.
- userbinator 12y agoOptimization algorithms will lead the result to a local minima, while missing a global minima. I think this is the one of the huge differences between humans and compilers that leads to suboptimal code - the compiler is completely unaware of what the programmer intended in the HLL source, so the best it can do is to generate code which it "knows" is the fastest way to implement exactly those statements; on the other hand, if you are a human writing an algorithm in Asm, you probably have things like instruction selection and register allocation already in mind and are willing to slightly change the "semantics" of your algorithm if it results in e.g. a better use of registers or the possibility to use a faster/smaller instruction. In other words, the compiler starts with something horribly unoptimised but following the semantics of the source language, and tries to make that as fast/small as it can while still maintaining the same semantics - and so this is the "local minima" it can reach. The human starts with a concept of an algorithm and its intended result, and everything in-between is flexible, so this is a more "global" view of the problem of code generation. The human isn't restricted by the semantics of the HLL, but can fully express the intended algorithm by exploiting the full capabilities of the processor.
- WalterBright 12y agoThe human also knows things that the compiler doesn't - such as which branches of the code are speed critical and which aren't. This matters for things like where precious registers get allocated.