What every compiler writer should know about programmers (2015) [pdf]
The author seems to be saying, in section 4, that compilers should just stop doing some optimizations (such as converting int to size_t where appropriate) and should expect programmers to do those optimizations instead, in order to reduce undefined behavior.<p>The problem with this argument is that it doesn&#x27;t match economic reality. Compiler authors wouldn&#x27;t disagree that it would be nice to not have to do these optimizations! (Chris Lattner has said as much to me, for example.) Unfortunately, there is a lot more application code out there than there is compiler code. So it makes economic sense to add optimizations to the compiler rather than to individual programs, so that the huge universe of C and C++ programs can benefit without having to be optimized by hand at the source level. This is why companies like Google and Apple employ compiler engineers: so that their large codebases can become faster automatically.<p>It&#x27;s trendy to complain about compiler engineers because they&#x27;re an easy target, and because of nostalgia for the days of Turbo Pascal when optimizations weren&#x27;t done and programs were slow. It&#x27;s much less trendy to analyze the complex circumstances that has led to UB exploitation in C and C++. In my opinion, if I had to assign blame to one thing, it would go to the C language itself, for e.g. encouraging use of int for array indices.<p>P.S. As always, Fabian Giesen&#x27;s description of why compilers exploit signed overflow is a must-read: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;rygorous&#x2F;e0f055bfb74e3d5f0af20690759de5a7" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;rygorous&#x2F;e0f055bfb74e3d5f0af20690759...</a><p>Note that the conclusion is <i>not</i> as simple as &quot;compilers should stop doing this optimization in C&quot;. There in fact isn&#x27;t a great solution precisely because int is 32 bits for popular ABIs in C; the best options are &quot;hope the programmer used size_t right&quot; or &quot;use another language&quot;.