[cfe-dev] struct copy

Argiris Kirtzidis akyrtzi at gmail.com
Mon Sep 22 08:30:10 PDT 2008


Given this program:

struct S { int x; };
void f() {
  struct S q1;
  struct S q2 = q1;
}


clang emits this:


    %struct.S = type <{ i32 }>

define void @f(...) nounwind {
entry:
    %q1 = alloca %struct.S, align 4        ; <%struct.S*> [#uses=1]
    %q2 = alloca %struct.S, align 4        ; <%struct.S*> [#uses=1]
    %tmp = bitcast %struct.S* %q2 to i8*        ; <i8*> [#uses=1]
    %tmp1 = bitcast %struct.S* %q1 to i8*        ; <i8*> [#uses=1]
    call void @llvm.memmove.i32(i8* %tmp, i8* %tmp1, i32 4, i32 4)
    br label %return

return:        ; preds = %entry
    ret void
}


Would it be better if it produced this instead ?:


define void @f(...) nounwind {
entry:
    %q1 = alloca %struct.S, align 4        ; <%struct.S*> [#uses=1]
    %q2 = alloca %struct.S, align 4        ; <%struct.S*> [#uses=1]
    %tmp = load %struct.S* %q1
    store %struct.S %tmp, %struct.S* %q2
    br label %return

return:        ; preds = %entry
    ret void
}


It's simpler and offers higher level information for those that want to 
examine the bitcode.

Isn't the job of the backend to interpret
    %tmp = load %struct.S* %q1
    store %struct.S %tmp, %struct.S* %q2
in an appropriate way according to target, thus not requiring the 
frontend to explicitly encode it as low-level block-of-bytes copy ?


-Argiris




More information about the cfe-dev mailing list