How does inlining work for ref parameters?

Janice Caron caron800 at googlemail.com
Sat May 17 03:03:58 PDT 2008


I get how inlining works for non-ref parameters. If I do

    int foo(int x, int y)
    {
        return x + y;
    }

    int a = ...;
    int b = ...;
    int c = foo(a,b);

Then this can turn into

    int a = ...;
    int b = ...;
        int x = a;
        int y = b;
        int t = a + b;
    int c = t;

which can subsequently be optimised to

    int a = ...;
    int b = ...;
    int c = a + b;

But I don't get how it all hangs together with reference arguments.
Here's an example:

    int bar(ref int* p, ref int* q)
    {
        return *p++ + *q++;
    }

    int* ap = ...;
    int* bp = ...;
    int c = bar(ap,bp);

My complete lack of understanding of how inlining works suggests to me
that this would translate into

    int* ap = ...;
    int* bp = ...;
        int** p = ≈
        int** q = &bp;
        int t = *(*p)++ + *(*q)++;
    int c = t;

which doesn't really optimise, except for the elimination of t. And
yet, we would /hope/ to end up with is:

    int* ap = ...;
    int* bp = ...;
    int c = *ap++ + *bp++;

This can certainly be achieved by another means. Specifically:

    string bar(string r, string p, string q)
    {
        return r~"= *"~p~"++ + *"~q~"++;"
    }

    int* ap = ...;
    int* bp = ...;
    int c;
    mixin(bar("c","ap","bp"));

But to my mind, the code is less readable.

So - can someone tell me - if a function takes lots of parameters
declared "ref", and that function is inlined, is the "ref" part
(passing the address and then dereferencing when needed) completely
eliminated, or not? Does anyone have a definitive answer?



More information about the Digitalmars-d mailing list