Skip to content

Dead Code Elimination Regression at -O1 (trunk vs. 14.0.0) #54980

@thetheodor

Description

@thetheodor

cat case.c

static int a, c;
static int *b = &a;

void bar126_(void);
void foo();

int main() {
  char f = 0;
  int g = *b;
  for (; f <= 0; f = 1) {
    int i = 0;
    if (c)
      break;
    if (f)
      foo();
    if (a)
      bar126_();
  }
  b = &c;
}

clang-084ad1ebeee2af9f7a9b464a346a7ca51d6f83dc (trunk) -O1 can not eliminate foo but clang-14.0.0 -O1 can.

Target: x86_64-unknown-linux-gnu


clang-084ad1ebeee2af9f7a9b464a346a7ca51d6f83dc (trunk) -O1 [-emit-llvm] -S -o /dev/stdout case.c

Emitted IR

; ModuleID = 'case.c'
source_filename = "case.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  br label %1

1:                                                ; preds = %5, %0
  %2 = phi i1 [ false, %0 ], [ true, %5 ]
  %3 = phi i1 [ true, %0 ], [ false, %5 ]
  br i1 %2, label %7, label %4

4:                                                ; preds = %1
  br i1 %3, label %5, label %6

5:                                                ; preds = %4, %6
  br label %1, !llvm.loop !5

6:                                                ; preds = %4
  call void (...) @foo() #2
  br label %5

7:                                                ; preds = %1
  ret i32 0
}

declare void @foo(...) local_unnamed_addr #1

attributes #0 = { nounwind uwtable "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "frame-pointer"="none" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project 084ad1ebeee2af9f7a9b464a346a7ca51d6f83dc)"}
!5 = distinct !{!5, !6, !7}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!"llvm.loop.unroll.disable"}

Reduced assembly

main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %rbp, -16
	movb	$1, %bl
	xorl	%ebp, %ebp
	.p2align	4, 0x90
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	testb	$1, %bpl
	jne	.LBB0_4
# %bb.2:                                #   in Loop: Header=BB0_1 Depth=1
	movb	$1, %bpl
	testb	$1, %bl
	movl	$0, %ebx
	jne	.LBB0_1
# %bb.3:                                #   in Loop: Header=BB0_1 Depth=1
	xorl	%ebx, %ebx
	xorl	%eax, %eax
	callq	foo@PLT
	jmp	.LBB0_1
.LBB0_4:
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main


clang-14.0.0 -O1 [-emit-llvm] -S -o /dev/stdout case.c

Emitted IR

; ModuleID = 'case.c'
source_filename = "case.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  ret i32 0
}

attributes #0 = { nounwind uwtable "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"uwtable", i32 1}
!2 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project 329fda39c507e8740978d10458451dcdb21563be)"}

Reduced assembly

main:                                   # @main
	.cfi_startproc
# %bb.0:
	xorl	%eax, %eax
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main


Bisection

Bisected to: a1f442b
Committed by: @nikic


clang-a1f442b2787ef63ba84c8627987cfebe3ba4f7f6 -O1 [-emit-llvm] -S -o /dev/stdout case.c

Emitted IR

; ModuleID = 'case.c'
source_filename = "case.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  br label %1

1:                                                ; preds = %5, %0
  %2 = phi i1 [ false, %0 ], [ true, %5 ]
  %3 = phi i1 [ true, %0 ], [ false, %5 ]
  br i1 %2, label %7, label %4

4:                                                ; preds = %1
  br i1 %3, label %5, label %6

5:                                                ; preds = %4, %6
  br label %1, !llvm.loop !3

6:                                                ; preds = %4
  call void (...) @foo() #2
  br label %5

7:                                                ; preds = %1
  ret i32 0
}

declare dso_local void @foo(...) local_unnamed_addr #1

attributes #0 = { nounwind uwtable "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "frame-pointer"="none" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"uwtable", i32 2}
!2 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project a1f442b2787ef63ba84c8627987cfebe3ba4f7f6)"}
!3 = distinct !{!3, !4, !5}
!4 = !{!"llvm.loop.mustprogress"}
!5 = !{!"llvm.loop.unroll.disable"}

Reduced assembly

main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %rbp, -16
	movb	$1, %bl
	xorl	%ebp, %ebp
	.p2align	4, 0x90
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	testb	$1, %bpl
	jne	.LBB0_4
# %bb.2:                                #   in Loop: Header=BB0_1 Depth=1
	movb	$1, %bpl
	testb	$1, %bl
	movl	$0, %ebx
	jne	.LBB0_1
# %bb.3:                                #   in Loop: Header=BB0_1 Depth=1
	xorl	%ebx, %ebx
	xorl	%eax, %eax
	callq	foo
	jmp	.LBB0_1
.LBB0_4:
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main


Previous commit: a84a8c9

clang-a84a8c937ba0455694ed59776b150b80a386d21b -O1 [-emit-llvm] -S -o /dev/stdout case.c

Emitted IR

; ModuleID = 'case.c'
source_filename = "case.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  ret i32 0
}

attributes #0 = { nounwind uwtable "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"uwtable", i32 2}
!2 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project a84a8c937ba0455694ed59776b150b80a386d21b)"}

Reduced assembly

main:                                   # @main
	.cfi_startproc
# %bb.0:
	xorl	%eax, %eax
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions