Christoffer Lernö via llvm-dev
2021-Sep-02 07:59 UTC
[llvm-dev] Zero init of structs and optimizations
I am investigating optimal LLVM IR from my language to LLVM. In this case zero initialization of structs. My understanding is that there are the following possible strategies. 1. Store a zero initializer. 2. Use memset to zero. 3. Create individual stores to zero for each struct field (on small structs, then use (1) or (2) for larger) I am looking at Clang, and it looks that with -O0 it follows (3), that is for smaller allocations it will create individual stores, but after some limit it outputs memset. It is unclear to me whether this is done for historical reasons *or* because it unifies handling of non-zero and zero cases *or* because this is the optimal IR for LLVM to perform optimizations on. An example: Using (1) %tempaddr = alloca %Vector2, align 4 store %Vector2 zeroinitializer, %Vector2* %tempaddr, align 4 Using (2) %literal = alloca %Vector2, align 4 %0 = bitcast %Vector2* %literal to i8* call void @llvm.memset.p0i8.i64(i8* align 4 %0, i8 0, i64 8, i1 false) Using (3) %1 = alloca %struct.Vector2, align 4 %2 = getelementptr inbounds %struct.Vector2, %struct.Vector2* %1, i32 0, i32 0 store float 0.000000e+00, float* %2, align 4 %3 = getelementptr inbounds %struct.Vector2, %struct.Vector2* %1, i32 0, i32 1 store float 0.000000e+00, float* %3, align 4 What should I prefer? /Christoffer