3 ms·
Not really. The two main reasons LLVM and gcc do a better optimization job is they have a better function inliner and loop unroller. Neither are particularly co
by WalterBright 8y ago
Not really. The two main reasons LLVM and gcc do a better optimization job is they have a better function inliner and loop unroller. Neither are particularly computation intensive.
DMD does all the usual data flow analysis optimizations - strength reduction, copy propagation, loop unrolling, dead store elimination, live range analysis, etc.
https://github.com/DigitalMars/Compiler/blob/master/dm/src/dmc/gflow.d https://github.com/DigitalMars/Compiler/blob/master/dm/src/d...
The optimizer / code generator is the same one that Digital Mars C++ uses, which was developed in the 1980's (!). Up until 2000 or so, the code generated was about the same as gcc, though DMC++ ran several times faster.
Since 2000, all my attention has been on the D front end, and so the optimizer wasn't getting the devoted attention it had earlier.
Just recently I finished converting the DMD optimizer from C to D, and am doing the code generator. This should make the code much more tractable.
- pcwalton 8y agoRespectfully—and I truly mean this!—this is nowhere near enough to attain parity with GCC and LLVM. I don't see any SSA form, to name just one of many issues. GCC 4.0, which introduced the SSA-based GIMPLE, was released in 2004, which is roughly around the same time you mentioned that the optimization quality started to diverge. This is not an accident. At the time, lots of compilers thought that they could continue to compete with GCC without doing SSA-based optimizations. GCC crushed them all one by one.
- WalterBright 8y agoI forgot to add SROA to the list. DMD does a limited form of SROA I wrote as a proof of concept, it should be generalized. https://github.com/DigitalMars/Compiler/blob/master/dm/src/dmc/gsroa.d https://github.com/DigitalMars/Compiler/blob/master/dm/src/d... I've also written a loop unroller, but it can be improved: https://github.com/DigitalMars/Compiler/blob/master/dm/src/dmc/gloop.d#L3759 https://github.com/DigitalMars/Compiler/blob/master/dm/src/d... My evaluation was based on comparing the code gen output of the various compilers, not looking at how they were achieved. As for SSA, DMD's optimizer is based on an intermediate representation that is a binary tree. A binary tree is a form of SSA - each operator node is a value assigned once and never modified. > Respectfully No worries. I'm used to people telling me I can't do things :-)
- judofyr 8y agoIs the intermediate representation documented/described somewhere?
- WalterBright 8y ago"Use the Source, Luke!" https://github.com/DigitalMars/Compiler/blob/master/dm/src/dmc/el.d https://github.com/DigitalMars/Compiler/blob/master/dm/src/d...
- judofyr 8y agoWell, I did look at it, but it was difficult to figure out how control-flow is structured. Is every “basic block” a single “el”, or is the CFG structured inside the binary tree? How are variables handled? Are there load expressions? Do you use something similar to phi-nodes to merge values from different branches? How do you do analysis? Do you walk over the expression tree and store the results (e.g. in constant propagation) on the nodes themselves? Do you use a separate cache/hashmap?
- WalterBright 8y agoBasic blocks form a linked list (block*), and each basic block has a tree of elem's. Control flow is done with the basic block "predecessor" and "successor" lists.