ldc
ldc copied to clipboard
Dead code produced in non optimized builds
For
bool b() {
union U { bool b; int i; }
U u;
return u.b;
}
LDC 1.32.1 produces
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]
}
%3
and %4
should not be emitted, that's dead code.
Non-optimized code is by definition sub-optimal. The double load is probably happening to simplify compiler implementation. I doubt whether it is worth spending time to eliminate it, as it will quickly be eliminated in during optimization.
Obviously DCE handles that very well but those two lines are a symptom of an internal bug.
Is it a bug, though, or just slightly suboptimal codegen in unoptimised mode? If you do suspect it's a symptom of an actual defect, I suggest you look through the internals (or -vv
) output to see what's actually going on.