ldc icon indicating copy to clipboard operation
ldc copied to clipboard

Non-optimized default initialization of small unions is sub optimal

Open ghost opened this issue 2 years ago • 1 comments
trafficstars

For

bool b()  {
        union U { bool b; int i; }
        U u;
        return u.b;
}

The IR produced with LDC 1.32.1 is

define zeroext i1 @_D7example1bFZb() #0 !dbg !4 {
  %u = alloca %example.b.U, align 4               ; [#uses = 2, size/byte = 4]
  %1 = bitcast %example.b.U* %u to i8*, !dbg !7   ; [#uses = 1] [debug line = app/example.d:3:9]
  call void @llvm.memset.p0i8.i64(i8* align 1 %1, i8 0, i64 4, i1 false), !dbg !7 ; [debug line = app/example.d:3:9]
  %2 = getelementptr inbounds %example.b.U, %example.b.U* %u, i32 0, i32 0 ; [#uses = 2, type = i8*]
  %3 = load i8, i8* %2, align 1, !dbg !8          ; [#uses = 1] [debug line = app/example.d:4:9]
  %4 = trunc i8 %3 to i1, !dbg !8                 ; [#uses = 0] [debug line = app/example.d:4:9]
  %5 = load i8, i8* %2, align 1, !dbg !8          ; [#uses = 1] [debug line = app/example.d:4:9]
  %6 = trunc i8 %5 to i1, !dbg !8                 ; [#uses = 1] [debug line = app/example.d:4:9]
  ret i1 %6, !dbg !8                              ; [debug line = app/example.d:4:9]
}

Insted of calling memset LDC should store 0 i32, even without optimizations.

ghost avatar May 18 '23 17:05 ghost

I want to mark this "won't fix". Can you elaborate on your rationale? Non-optimized code is by definition sub-optimal, helping reduce compiler implementation complexity (and decreasing chance of codegen bugs for corner cases).

JohanEngelen avatar May 19 '23 08:05 JohanEngelen