[cfe-dev] Redundant byval in C codegen?

Rafael Espindola espindola at google.com
Tue Jan 19 17:41:38 PST 2010

2010/1/19 Zhongxing Xu <xuzhongxing at gmail.com>:
> Does that mean that if we created a temporary for a struct argument,
> 'byval' attribute should not be used to avoid an alloca?

In some cases we do that (small structs). In some cases you don't have
a choice (ABI). In others not using a byval will bloat the IL with
lots of scalar arguments. Not using a byval might also worsen the
callee code since the arguments will be on the stack but nothing
before the codegen will know that.

My idea is that if

struct s  {int a; int b;  int c; int d int e;};
void g(struct s a);
void f() {
  struct s a = {1, 2, 3, 4, 5};

Could be compiled to something like
%struct.s = type { i32, i32, i32, i32, i32 }

define void @f() {
  call void @g({ i32 1, i32 2, i32 3, i32 4, i32 5})
  ret void

declare void @g(%struct.s* byval)

We would have the nice properties
*) It is clear that the caller can store the the struct temporary in
any way. In fact, it can optimize it away
*) It is clear that the callee will get the structure via a memory pointer

It looks strange at first to have call pass a value to a pointer, but
since it copies the argument, that probably is not a problem.

Rafael Ávila de Espíndola

More information about the cfe-dev mailing list