pure-ifying my code

Jonathan M Davis jmdavisProg at gmx.com
Sun Nov 17 23:37:11 PST 2013


On Monday, November 18, 2013 08:22:37 qznc wrote:
> On Sunday, 17 November 2013 at 21:00:16 UTC, Jonathan M Davis
> 
> wrote:
> > will definitely result in multiple calls to pure_func. It's not
> > that it's
> > impossible for the compiler to do it - it's perfectly possible.
> > But doing so
> > would require at least basic code flow analysis, and dmd almost
> > never does any
> > kind of code flow analysis.
> 
> I remember Walter being against flow analysis in the frontend.
> This is middleend optimization stuff, though. Does dmd really use
> no data flow analysis anywhere?

It has very, very limited flow analysis that it uses in constructors to avoid 
multiple calls to super as well as guaranteeing that nothing gets initialized 
multiple times in a const or immutable constructor. It may also does some very 
minimal flow analysis when dealing with value range propagation ( 
http://www.drdobbs.com/tools/value-range-propagation/229300211 ). It may also 
do some minimal checks for missing return statements. But it general, no, it 
doesn't do flow analysis, and when it does, it's very minimal. Walter does not 
consider it to be worth the complexity that it requires.

And while in this particular case, it would certainly be much nicer for 
repeated calls to a pure function to be optimized within an entire function 
instead of just within an expression, I don't think that it actually ends up 
mattering much in practice, simply because it's generally rare to make the 
exact same call multiple times within a function. And if you do, it's trivial 
to save the result in a variable to be reused.

> This is middleend optimization stuff, though.

I'm not quite sure what you mean by this. There is no middle-end. We have the 
front-end, which is shared by dmd, gdc, and ldc, and then each compiler has 
its own backend. Anything D-specific is done in the front-end. So, if there are 
any D-specific optimizations (such as optimizations related to pure), they need 
to be in the front-end.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list