On 03/06/14 05:24, Alec Teal wrote: > On 27/05/14 10:40, Andrew Haley wrote: >> On 05/25/2014 01:32 PM, Niklas Gürtler wrote: >>> Hello GCC List, >>> >>> i am currently working on a hardware API in C++11 for ARM Cortex-M3 >>> microcontrollers. It provides an object oriented way of accessing >>> hardware registers. The idea is that the user need not worry about >>> individual registers and their composition of bit fields but can access >>> these with symbolic names. >>> The API uses temporary objects and call chaining for syntactic sugar. >>> The problem is now that GCC produces correct, but way too slow and too >>> much code. >>> >>> See the attached simplified testcase (with a dummy linker script to >>> shorten disassembler output) and the function getInput. When compiling >>> with gcc-arm-embedded ( https://launchpad.net/gcc-arm-embedded ), this >>> is the code generated by GCC: > [snip] >> >> But really, I think you are going down the wrong path. If you want GCC >> to generate tight code, you should write tight code. Don't write lots >> of pointless stuff in the hope that GCC will notice it's pointless. >> Maybe it will, maybe not. Your API is rather complicated for what it >> does. You should be able to write it in a way that is less work. >> > I agree with Andrew here only in part. You shouldn't worry about hitting > compiler optimisations as you write code, but you shouldn't convolute > code as a test either BTW. When you want to improve performance I urge > you to actually profile code, maybe learn how to do hypothesis tests, > you want to be able to say "it slows down here" and "after doing X it > now runs Y times better" and things like that. > > It is odd that GCC misses this. You missed part of what I said: GCC today does not miss this. It now generates tight code for the example. But I'm going to stand by what I said. There is a tendency to write layers and layers of (object-oriented) abstraction which don't do anything useful, and assume that the compiler will optimize it all away. But that sometimes serves only to obfuscate the code, both for the compiler and the (human) reader. It is true that a compiler can often elide that stuff, but it is also true that if you add more layers of abstraction, eventually the compiler will find something it can't see through and it'll generate actual code for all that stuff. I really don't think it's a good attitude for a programmer to have that you can write code in the knowledge that not only will it not be executed, it probably won't even cause the compiler to generate anything. Andrew.