6 ms·
Optimizing C++ Code: Dead Code Elimination
- eliasmacpherson 13y agoGot stung by this testing out of idle curiousity whether strncmp or memcmp was faster the other day.
- deletes 13y agoSo memcmp was faster, right?
- eliasmacpherson 13y agoDidn't finish the job, realised code was optimised out by O3 and left it at that. Unoptimised strncmp was beating unoptimised memcmp, it was taking 1.900s to finish where memcmp was taking 2.178s over many iterations.
- octo_t 13y agoDead Code Elimination can even optimise out blocking loops, a loop like for(;;) can be optimised out.
- dman 13y agoSo the compiler writers are bringing lazy evaluation to the unsuspecting masses?
- octo_t 13y agoThats not lazy evaluation so much as "this loop literally doesn't do anything"
- jerf 13y agoIt does do something. It loops forever. The optimized version does nothing. This is a major difference in performance. No sarcasm; this sort of thing can manifest in real code, and when it does it will generally be a great deal more complicated. "Bugs that only appear at certain optimization levels" are not the absolute worst types of bugs to debug, but they're certainly up there on the great Hierarchy of Sucky Bugs.
- garenp 13y agoIf an empty loop does no computation (i.e. has no observable side-effects), then arguably there should be no difference in observable behavior from the program. Of course, sometimes we "know" something that the compiler can't "see", and so must indicate as such in our code (e.g. using something stored inside the loop, adding volatile, ...). Probably the most common (or at least my favorite) example of DCE leading to a surprise, is when embedded programmers use so-called "delay loops" which can be eliminated by the compiler because it too is "empty" (the loop produces nothing that is used outside of the loop).
- scott_s 13y agoAn empty loop that has no computation but loops forever has an observable side-effect.
- nhaehnle 13y agoGo read page 14 of this: http://isocpp.org/files/papers/N3690.pdf http://isocpp.org/files/papers/N3690.pdf ;-) Essentially, C++ does not consider looping forever to be a side effect. You have to do something like writing to a volatile variable to ensure that the compiler does what you want.
- gizmo686 13y agoThe relevent part of page 14: "The implementation may assume that any thread will eventually do one of the following: — terminate, — make a call to a library I/O function, — access or modify a volatile object, or — perform a synchronization operation or an atomic operation. [ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. — end note ]"
- knz42 13y agoI don't believe that's right (see below why). Point me to one compiler which does this intentionally (ie not a bug) and I would stand corrected, but in most cases most compiler experts I know (myself included) would frown and disapprove. There was a bug in GCC a while ago to that effect, but it's been corrected since. Why: Computationally, there is no difference between "no code" and "a loop that iterates 100 times and does nothing". It is thus correct to eliminate the latter to preserve the observable behaviour. However, a loop that never terminates is a very different thing entirely: it says "execution will never progress past this point" and everything afterwards will never be reached. This property is so enshrined in theoretical computer science that any half-decent verification tool will consider what comes after the loop as dead code and not check it. In the newer GCC and Clang the code after a never-ending loop will be marked "unreachable" and be killed as dead code. So removing a never-ending loop would be quite the big deal indeed. Technical note: specifically in C and C++ and every language implemented using them, a loop with a counter but no check on the upper bound (eg. "for(i = 0; ; ++i)" ) is very different from the empty loop discussed above. Such a loop would be called "undefined behavior": the program may or may not stop, the world may explode, etc. This is because C and C++ say that overflow has undefined behavior. So this kind of loop does not mean "the programmer intends the control flow to terminate here", but rather "the programmer is playing with fire and it can burn".
- nhaehnle 13y agoWhat you write is perfectly plausible, and yet you're wrong. Such is the magic of the C++ standard ;) There is a part of the C++ standard that says something along the lines of: the compiler may assume that every program will eventually exhibit some side-effect. That part of the standard also explicitly states that this is intended to allow compilers to remove loops with empty bodies (or bodies with only dead code) even if they cannot prove termination of the loop. See e.g. page 14 of http://isocpp.org/files/papers/N3690.pdf http://isocpp.org/files/papers/N3690.pdf This is certainly useful when you think of code that is created indirectly via macros and/or template instantiation. Whether compilers actually perform this "optimization" on explicit endless loops like for(;;); is another question, but at least C++ can claim to be able to run certain endless loops in under a second ;)
- beerscout 13y agoSomewhat related: http://blog.regehr.org/archives/140 http://blog.regehr.org/archives/140
- aa0 13y agoMSVC, barely supports C99 but has time to have devs make posts about the obvious nature of optimization. Sigh.
- gecko 13y agoThis gets repeated a lot, but I'll say it again anyway: MSVC is a C++ compiler. It happens to support some C99 (and will support more in VS2013, actually), but that's not its focus. While I, and many other Windows developers, feel that its standard compliance has fallen a bit behind in the last five years or so, Microsoft has made tremendous strides towards fixing that problem lately, and the roadmap (http://blogs.msdn.com/b/somasegar/archive/2013/06/28/10429934.aspx?Redirected=true http://blogs.msdn.com/b/somasegar/archive/2013/06/28/1042993...) for full C++11/14 compliance looks good, and seems reasonable. Further, while C++11/14 compliance in MSVC isn't amazing, its compilation speed, and the optimizations it can deliver to its compiled executables, are both top-notch. VC++ generally seems to land between GCC and ICC for most code I write. Is it perfect? No. Should it support more of C++11 and 14? Yes, and they're working on it. But as much as I wish there were a great C99 compiler on Windows, Microsoft has been open for a very, very long time that that's simply not their focus. Just as I don't assail Apple for Xcode's lousy Python support, I don't see a point in attacking MSVC for not doing C support.
- tveita 13y agoIt would help if they would just declare it unmaintained. Maybe projects would give up on sticking to a 20 year old C standard just to maintain compatibility with MSVC.
- sharth 13y agoPersonally, I'd expect a compiler to take it one step further than what was suggested here. For example, clang or gcc (under -O1) will precompute all of the math in that for-loop, and the entire function just becomes the printf with a known value. define i32 @main() nounwind uwtable ssp { %1 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([6 x i8]* @.str, i64 0, i64 0), i64 500000000500000000) nounwind ret i32 0 }
- CurtHagenlocher 13y agoIf you read all the way into the comments, you'll see that this does happen -- but only for values of n that are quite small. And this is a side effect of other optimizations -- not because of anything specific to this closed form.
- sharth 13y agoYeah, I was just now skimming through the comments as well and I saw that. It looks like their constant propagation logic doesn't work on loops at all, and it's just that their loop unroller stage (which runs before it) will unroll loops of less than 10 iterations.
- DannyBee 13y agoYes, which is kinda sad. both GCC and LLVM do it as a result of computing scalar evolutions for the variables, and then instantiating the final values after the loop. There are certain calculations both will bail on due to solving cost (rather than inability to solve), but otherwise ...