Passing structs byval

Hi,

I'm looking at the argpromotion optimizer pass in LLVM, but it doesn't seem to
reliably work right now in combination with clang (didn't try with llvm-gcc).

When passing a struct to a function, clang currently generates a call to
memcpy and passes the pointer to the (temp copy of the) struct to the
function. ArgPromotion can succesfully promote this struct argument (i.e.,
replace it by multiple scalar arguments) but only if the called function does
not modify the struct.

Now, on to my actual question. Why does clang do this memcpy trick in the
frontend? Wouldn't it be more flexible to simply pass the struct pointer byval
to the function and let the codegenerator or a later optimization step add the
memcpy (if still needed)? Is this a deliberate choice (perhaps because the
codegen doesn't support byval properly yet?), or is it simply generic
handling of structs that causes this (from looking at the code, it looks like
this).

Any suggestions on how and where to implement this byval passing of structs?
I'm still digging around in the source code, following call paths to see what
actually happens right now :slight_smile:

Gr.

Matthijs

I'm looking at the argpromotion optimizer pass in LLVM, but it doesn't seem to
reliably work right now in combination with clang (didn't try with llvm-gcc).

Ok. Most optimization stuff has only been looked at with llvm-gcc.

When passing a struct to a function, clang currently generates a call to
memcpy and passes the pointer to the (temp copy of the) struct to the
function. ArgPromotion can succesfully promote this struct argument (i.e.,
replace it by multiple scalar arguments) but only if the called function does
not modify the struct.

I don't think there is any specific reason for this, it is just what was easiest to do at the time. We should follow llvm-gcc's lead and generate byval.

Any suggestions on how and where to implement this byval passing of structs?
I'm still digging around in the source code, following call paths to see what
actually happens right now :slight_smile:

CodeGen/CGExprAgg.cpp and friends is the most relevant. Thanks for working on this!

-Chris