To help LDC/GDC

Iain Buclaw ibuclaw at ubuntu.com
Mon Apr 8 05:52:09 PDT 2013


On 8 April 2013 13:25, Jacob Carlborg <doob at me.com> wrote:

> On 2013-04-08 10:29, Iain Buclaw wrote:
>
>  This information could possibly be helpful.  Though given that most of
>> (gdc) codegen is on par with g++, there's probably not much on the list
>> that isn't already detected by the backend optimisation passes.
>>
>
> Multiple calls to pure functions could be cached.
>
> --
> /Jacob Carlborg
>

Not always, but in some circumstances, yes.

---
struct Foo
{
  int a = 0;
  pure int bar (immutable int x)
  {
    ++a;
    return x * 2;
  }
}


void main()
{
  Foo f;
  int i = f.bar(2) + f.bar(2);

  assert (i == 8);
  assert (f.a == 2);
}
---


Again, the characteristics of D pure functions mean that the backend can
const-fold away the function entirely under normal optimisations.

---
D main ()
{
  <bb 2>:
  # DEBUG f.a => 0
  # DEBUG f.a => 1
  # DEBUG f.a => 2
  # DEBUG i => 8
  return 0;
}
---


Possibly strongly-pure functions could be marked as pure in the 'C' sense,
but only if they are also nothrow, eg to add to the above example:

---
pure nothrow int baz (Foo f)
{
  return f.bar(2) + f.bar(2);
}
---


Regards
-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130408/7cef1fe9/attachment.html>


More information about the Digitalmars-d mailing list