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

edA-qa mort-ora-y via llvm-dev llvm-dev at lists.llvm.org
Wed Apr 18 09:40:01 PDT 2018


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?

    define i32 @main() #0 {
      %v = alloca %struct.vpt_data, align 4
      %1 = alloca { i64, float }, align 4
      %2 = bitcast { i64, float }* %1 to i8*
      %3 = bitcast %struct.vpt_data* %v to i8*
      call void @llvm.memcpy.p0i8.p0i8.i64(i8* %2, i8* %3, i64 12, i32
4, i1 false)
      %4 = getelementptr inbounds { i64, float }, { i64, float }* %1,
i32 0, i32 0
      %5 = load i64, i64* %4, align 4
      %6 = getelementptr inbounds { i64, float }, { i64, float }* %1,
i32 0, i32 1
      %7 = load float, float* %6, align 4
      call void @vpt_test(i64 %5, float %7)
      ret i32 0
    }


-- 
edA-qa mort-ora-y  
	http://mortoray.com/

Creator of the Leaf language
	http://leaflang.org/

Streaming algorithms, AI, and design on Twitch
	https://www.twitch.tv/mortoray

Twitter
	edaqa
	



More information about the llvm-dev mailing list