llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Dead Code Elimination Regression at -Oz (trunk vs. 13.0.1)

Open Quarub opened this issue 3 years ago • 1 comments

cat case.c #226621

int b, c;
short f;
void foo();
short(a)(short g) { return g; }
int(d)(int g, int i) {
  return g && g > 7 - i || g < 0 && i < 0 && g ? 0 : g + i;
}
char(e)();
static unsigned short h() {
  for (; b <= 4; b = d(b, 2)) {
    int j = 0;
    for (;; c = 1) {
      a(48328);
      if (c)
        break;
      f = a(1);
      if (!d(j, f)){
        j = e();
        foo();
      }
    }
  }
  return b;
}
int main() { h(); }

llvm-70b8b738c5794799e9807549e5058d9570176918 (trunk) -Oz can not eliminate foo but llvm-llvmorg-13.0.1 -Oz can.

Target: x86_64-unknown-linux-gnu


llvm-70b8b738c5794799e9807549e5058d9570176918 (trunk) -Oz -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"

@b = dso_local local_unnamed_addr global i32 0, align 4
@c = dso_local local_unnamed_addr global i32 0, align 4
@f = dso_local local_unnamed_addr global i16 0, align 2

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize readnone willreturn uwtable
define dso_local signext i16 @a(i16 noundef returned signext %0) local_unnamed_addr #0 {
  ret i16 %0
}

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize readnone willreturn uwtable
define dso_local i32 @d(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp ne i32 %0, 0
  %4 = sub nsw i32 7, %1
  %5 = icmp slt i32 %4, %0
  %6 = select i1 %3, i1 %5, i1 false
  br i1 %6, label %13, label %7

7:                                                ; preds = %2
  %8 = and i32 %1, %0
  %9 = icmp slt i32 %8, 0
  %10 = and i1 %3, %9
  %11 = add nsw i32 %1, %0
  %12 = select i1 %10, i32 0, i32 %11
  br label %13

13:                                               ; preds = %7, %2
  %14 = phi i32 [ 0, %2 ], [ %12, %7 ]
  ret i32 %14
}

; Function Attrs: minsize nounwind optsize uwtable
define dso_local i32 @main() local_unnamed_addr #1 {
  %1 = load i32, ptr @b, align 4, !tbaa !5
  br label %2

2:                                                ; preds = %19, %0
  %3 = phi i32 [ %21, %19 ], [ %1, %0 ]
  %4 = icmp slt i32 %3, 5
  br i1 %4, label %5, label %22

5:                                                ; preds = %2
  %6 = load i32, ptr @c, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %17, %5
  %8 = phi i32 [ %6, %5 ], [ 1, %17 ]
  %9 = phi i32 [ 0, %5 ], [ %18, %17 ]
  %10 = icmp eq i32 %8, 0
  br i1 %10, label %11, label %19

11:                                               ; preds = %7
  store i16 1, ptr @f, align 2, !tbaa !9
  %12 = tail call i32 @d(i32 noundef %9, i32 noundef 1) #3
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %17

14:                                               ; preds = %11
  %15 = tail call signext i8 (...) @e() #4
  %16 = sext i8 %15 to i32
  tail call void (...) @foo() #4
  br label %17

17:                                               ; preds = %14, %11
  %18 = phi i32 [ %9, %11 ], [ %16, %14 ]
  store i32 1, ptr @c, align 4, !tbaa !5
  br label %7

19:                                               ; preds = %7
  %20 = load i32, ptr @b, align 4, !tbaa !5
  %21 = tail call i32 @d(i32 noundef %20, i32 noundef 2) #3
  store i32 %21, ptr @b, align 4, !tbaa !5
  br label %2, !llvm.loop !11

22:                                               ; preds = %2
  ret i32 0
}

; Function Attrs: minsize optsize
declare signext i8 @e(...) local_unnamed_addr #2

; Function Attrs: minsize optsize
declare void @foo(...) local_unnamed_addr #2

attributes #0 = { minsize mustprogress nofree norecurse nosync nounwind optsize readnone willreturn 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 = { minsize nounwind optsize 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 #2 = { minsize optsize "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 #3 = { minsize optsize }
attributes #4 = { minsize nounwind optsize }

!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 16.0.0 (https://github.com/llvm/llvm-project.git 70b8b738c5794799e9807549e5058d9570176918)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}

llvm-llvmorg-13.0.1 -Oz -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"

@b = dso_local local_unnamed_addr global i32 0, align 4
@c = dso_local local_unnamed_addr global i32 0, align 4
@f = dso_local local_unnamed_addr global i16 0, align 2

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize readnone uwtable willreturn
define dso_local signext i16 @a(i16 returned signext %0) local_unnamed_addr #0 {
  ret i16 %0
}

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize readnone uwtable willreturn
define dso_local i32 @d(i32 %0, i32 %1) local_unnamed_addr #0 {
  %3 = icmp ne i32 %0, 0
  %4 = sub nsw i32 7, %1
  %5 = icmp slt i32 %4, %0
  %6 = select i1 %3, i1 %5, i1 false
  br i1 %6, label %14, label %7

7:                                                ; preds = %2
  %8 = icmp slt i32 %0, 0
  %9 = icmp slt i32 %1, 0
  %10 = select i1 %8, i1 %9, i1 false
  %11 = and i1 %3, %10
  %12 = add nsw i32 %1, %0
  %13 = select i1 %11, i32 0, i32 %12
  br label %14

14:                                               ; preds = %7, %2
  %15 = phi i32 [ 0, %2 ], [ %13, %7 ]
  ret i32 %15
}

; Function Attrs: minsize nounwind optsize uwtable
define dso_local i32 @main() local_unnamed_addr #1 {
  %1 = load i32, i32* @b, align 4, !tbaa !3
  br label %2

2:                                                ; preds = %11, %0
  %3 = phi i32 [ %15, %11 ], [ %1, %0 ]
  %4 = icmp slt i32 %3, 5
  br i1 %4, label %5, label %16

5:                                                ; preds = %2
  %6 = load i32, i32* @c, align 4, !tbaa !3
  br label %7

7:                                                ; preds = %10, %5
  %8 = phi i32 [ %6, %5 ], [ 1, %10 ]
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %7
  store i16 1, i16* @f, align 2, !tbaa !7
  store i32 1, i32* @c, align 4, !tbaa !3
  br label %7

11:                                               ; preds = %7
  %12 = load i32, i32* @b, align 4, !tbaa !3
  %13 = icmp sgt i32 %12, 5
  %14 = add nsw i32 %12, 2
  %15 = select i1 %13, i32 0, i32 %14
  store i32 %15, i32* @b, align 4, !tbaa !3
  br label %2, !llvm.loop !9

16:                                               ; preds = %2
  ret i32 0
}

attributes #0 = { minsize mustprogress nofree norecurse nosync nounwind optsize readnone uwtable willreturn "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 = { minsize nounwind optsize 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 13.0.1 (https://github.com/llvm/llvm-project.git 75e33f71c2dae584b13a7d1186ae0a038ba98838)"}
!3 = !{!4, !4, i64 0}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
!7 = !{!8, !8, i64 0}
!8 = !{!"short", !5, i64 0}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}

Bisection

Bisected to: 1b1c8d83d3567a60280291c0adb95d1d60335509 Committed by: @hyeongyukim


llvm-1b1c8d83d3567a60280291c0adb95d1d60335509 -Oz -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"

@b = dso_local local_unnamed_addr global i32 0, align 4
@c = dso_local local_unnamed_addr global i32 0, align 4
@f = dso_local local_unnamed_addr global i16 0, align 2

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize readnone uwtable willreturn
define dso_local signext i16 @a(i16 noundef returned signext %0) local_unnamed_addr #0 {
  ret i16 %0
}

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize readnone uwtable willreturn
define dso_local i32 @d(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp ne i32 %0, 0
  %4 = sub nsw i32 7, %1
  %5 = icmp slt i32 %4, %0
  %6 = select i1 %3, i1 %5, i1 false
  br i1 %6, label %13, label %7

7:                                                ; preds = %2
  %8 = and i32 %1, %0
  %9 = icmp slt i32 %8, 0
  %10 = and i1 %3, %9
  %11 = add nsw i32 %1, %0
  %12 = select i1 %10, i32 0, i32 %11
  br label %13

13:                                               ; preds = %7, %2
  %14 = phi i32 [ 0, %2 ], [ %12, %7 ]
  ret i32 %14
}

; Function Attrs: minsize nounwind optsize uwtable
define dso_local i32 @main() local_unnamed_addr #1 {
  %1 = load i32, i32* @b, align 4, !tbaa !3
  br label %2

2:                                                ; preds = %19, %0
  %3 = phi i32 [ %21, %19 ], [ %1, %0 ]
  %4 = icmp slt i32 %3, 5
  br i1 %4, label %5, label %22

5:                                                ; preds = %2
  %6 = load i32, i32* @c, align 4, !tbaa !3
  br label %7

7:                                                ; preds = %17, %5
  %8 = phi i32 [ %6, %5 ], [ 1, %17 ]
  %9 = phi i32 [ 0, %5 ], [ %18, %17 ]
  %10 = icmp eq i32 %8, 0
  br i1 %10, label %11, label %19

11:                                               ; preds = %7
  store i16 1, i16* @f, align 2, !tbaa !7
  %12 = tail call i32 @d(i32 noundef %9, i32 noundef 1) #3
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %17

14:                                               ; preds = %11
  %15 = tail call signext i8 (...) @e() #3
  %16 = sext i8 %15 to i32
  tail call void (...) @foo() #3
  br label %17

17:                                               ; preds = %14, %11
  %18 = phi i32 [ %9, %11 ], [ %16, %14 ]
  store i32 1, i32* @c, align 4, !tbaa !3
  br label %7

19:                                               ; preds = %7
  %20 = load i32, i32* @b, align 4, !tbaa !3
  %21 = tail call i32 @d(i32 noundef %20, i32 noundef 2) #3
  store i32 %21, i32* @b, align 4, !tbaa !3
  br label %2, !llvm.loop !9

22:                                               ; preds = %2
  ret i32 0
}

; Function Attrs: minsize optsize
declare dso_local signext i8 @e(...) local_unnamed_addr #2

; Function Attrs: minsize optsize
declare dso_local void @foo(...) local_unnamed_addr #2

attributes #0 = { minsize mustprogress nofree norecurse nosync nounwind optsize readnone uwtable willreturn "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 = { minsize nounwind optsize 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 #2 = { minsize optsize "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 #3 = { minsize nounwind optsize }

!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.git 1b1c8d83d3567a60280291c0adb95d1d60335509)"}
!3 = !{!4, !4, i64 0}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
!7 = !{!8, !8, i64 0}
!8 = !{!"short", !5, i64 0}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}

Previous commit: e7c8cd4a930465edd6c0ecbf3ad3119737988941

llvm-e7c8cd4a930465edd6c0ecbf3ad3119737988941 -Oz -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"

@b = dso_local local_unnamed_addr global i32 0, align 4
@c = dso_local local_unnamed_addr global i32 0, align 4
@f = dso_local local_unnamed_addr global i16 0, align 2

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize readnone uwtable willreturn
define dso_local signext i16 @a(i16 returned signext %0) local_unnamed_addr #0 {
  ret i16 %0
}

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize readnone uwtable willreturn
define dso_local i32 @d(i32 %0, i32 %1) local_unnamed_addr #0 {
  %3 = icmp ne i32 %0, 0
  %4 = sub nsw i32 7, %1
  %5 = icmp slt i32 %4, %0
  %6 = select i1 %3, i1 %5, i1 false
  br i1 %6, label %14, label %7

7:                                                ; preds = %2
  %8 = icmp slt i32 %0, 0
  %9 = icmp slt i32 %1, 0
  %10 = select i1 %8, i1 %9, i1 false
  %11 = and i1 %3, %10
  %12 = add nsw i32 %1, %0
  %13 = select i1 %11, i32 0, i32 %12
  br label %14

14:                                               ; preds = %7, %2
  %15 = phi i32 [ 0, %2 ], [ %13, %7 ]
  ret i32 %15
}

; Function Attrs: minsize nounwind optsize uwtable
define dso_local i32 @main() local_unnamed_addr #1 {
  %1 = load i32, i32* @b, align 4, !tbaa !3
  %2 = load i32, i32* @c, align 4, !tbaa !3
  br label %3

3:                                                ; preds = %11, %0
  %4 = phi i32 [ %8, %11 ], [ %2, %0 ]
  %5 = phi i32 [ %14, %11 ], [ %1, %0 ]
  %6 = icmp slt i32 %5, 5
  br i1 %6, label %7, label %15

7:                                                ; preds = %3, %10
  %8 = phi i32 [ 1, %10 ], [ %4, %3 ]
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %7
  store i16 1, i16* @f, align 2, !tbaa !7
  store i32 1, i32* @c, align 4, !tbaa !3
  br label %7

11:                                               ; preds = %7
  %12 = icmp sgt i32 %5, 5
  %13 = add nsw i32 %5, 2
  %14 = select i1 %12, i32 0, i32 %13
  store i32 %14, i32* @b, align 4, !tbaa !3
  br label %3, !llvm.loop !9

15:                                               ; preds = %3
  ret i32 0
}

attributes #0 = { minsize mustprogress nofree norecurse nosync nounwind optsize readnone uwtable willreturn "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 = { minsize nounwind optsize 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.git e7c8cd4a930465edd6c0ecbf3ad3119737988941)"}
!3 = !{!4, !4, i64 0}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
!7 = !{!8, !8, i64 0}
!8 = !{!"short", !5, i64 0}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}

Quarub avatar Jul 27 '22 20:07 Quarub

@hyeongyukim any chance you could take a look as this has been bisected to one of your changes?

fhahn avatar Aug 15 '22 07:08 fhahn