Nested function calls to mutually recursive functions

Hello to all,

My situation is similar to this question over here: http://lists.llvm.org/pipermail/llvm-dev/2010-June/032684.html. Specifically, I would like to support a front-end that compiles from a language with nested, probably mutually-recursive to LLVM IR. Before reading the above thread, I was thinking about finding fixed points of free variables, etc, but the static links seem to avoid all the fuss. However, I have two further questions regarding that technique:

  1. I am not very sure I have grasped the way the author suggests augmenting the AST. I have the following thought problems: how would I augment a Function Call to indicate the changes that arise from the nesting? Only pass as an extra first argument the static link structure of the parent? Is this enough, or something more is needed?

  2. What kind of combination of LLVM passes (and in which turn, since this seems to be important in some aspects) can I apply in order to get rid of all those “getelementptr” instructions, and LLVM can replace arguments with the correct ones, similar to the analysis I was intending to do, with finding fixed points, etc? Or isn’t that possible at all, or only to some extent?

Thanks,
Jason

Hello to all,

>
> My situation is similar to this question over here:
> http://lists.llvm.org/pipermail/llvm-dev/2010-June/032684.html.
> Specifically, I would like to support a front-end that compiles from a
> language with nested, probably mutually-recursive to LLVM IR. Before
> reading the above thread, I was thinking about finding fixed points of free
> variables, etc, but the static links seem to avoid all the fuss. However, I
> have two further questions regarding that technique:
>
> 1) I am not very sure I have grasped the way the author suggests augmenting
> the AST. I have the following thought problems: how would I augment a
> Function Call to indicate the changes that arise from the nesting? Only
> pass as an extra first argument the static link structure of the parent? Is
> this enough, or something more is needed?
>
> 2) What kind of combination of LLVM passes (and in which turn, since this
> seems to be important in some aspects) can I apply in order to get rid of
> all those "getelementptr" instructions, and LLVM can replace arguments with
> the correct ones, similar to the analysis I was intending to do, with
> finding fixed points, etc? Or isn't that possible at all, or only to
> some extent?

I would need to see (a sketch of) the IR but I imagine the following:

Assuming you will not inline everything, you probably want to run normal
cleanup passes (SROA, instcombine, simplifycfg, ...) and then
ArgumentPromotion (or the Attributor) to break the passed struct
pointers apart and pass the values of the members instead. IPSCCP (or
the Attributor) can also forward constant values from the callee to the
caller once the struct is broken up.

Cheers,
Johannes