[llvm-dev] Why does clang do a memcpy? Is the cast not enough? (ABI function args)

Dimitry Andric via llvm-dev llvm-dev at lists.llvm.org
Wed Apr 18 10:13:29 PDT 2018


On 18 Apr 2018, at 18:40, edA-qa mort-ora-y via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> I'm implementing function arguments and tested this code in C:
> 
>     // clang -emit-llvm ll_struct_arg.c -S -o /dev/tty
>     typedef struct vpt_data {
>         char a;
>         int b;
>         float c;
>     } vpt_data;
> 
>     void vpt_test( vpt_data vd ) {
>     }
> 
>     int main() {
>         vpt_data v;
>         vpt_test(v);
>     }
> 
> This emits an odd LLVM structure that casts to the desired struct type,
> but also memcpy's to a temporary structure. I'm unsure of why the memcpy
> is done as opposed to just casting directly?

Because you are passing the parameter by value?  It *should* copy the
data.  In this particular case it will probably be elided if you turn on
optimization, but it is more logical to pass structs via a const
reference or pointer.

-Dimitry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 223 bytes
Desc: Message signed with OpenPGP
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180418/eff40549/attachment.sig>


More information about the llvm-dev mailing list