Issue 64679
Summary Dead Code Elimination Regression at -O3 (trunk vs. llvmorg-16.0.6)
Labels new issue
Assignees
Reporter SvenjaScherrer
    ```c
static int b;
static short c = 5, e;
static char ah, am, f;
void foo(void);
void bar155_(void);
void bar149_(void);
static short(a)(short g, short i) { return g + i; }
static short(d)(short g, short i) { return g - i; }
static void ab() { b = 2905239065; }
static void h(int g) {
  int ar = g;
  ab();
  am = 0;
  for (; am > -7; am = d(am, 6)) {
 if (ar)
      bar155_();
    if (ar < a(g, 0 > b))
      ;
 else
      continue;
    e = 61690;
    c = ah;
    ar = e;
 }
  if (!ar)
    bar149_();
}
int main() {
  f = b;
 h(f);
  if (c)
    foo();
}
```

`clang-87a8e2247536 (trunk) -O3` cannot eliminate the call to foo but `clang-llvmorg-16.0.6 -O3` can.

-----------------------------------------------------------------------

`clang-87a8e22475364b79d43515159de1b7947554d868 -O3 case.c -S -emit-llvm -o case.ll`
<details ><summary>Emitted IR</summary><p>

```ll
; 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 = internal unnamed_addr global i1 false, align 4
@c = internal unnamed_addr global i1 false, align 2
@am = internal unnamed_addr global i8 0, align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  %1 = load i1, ptr @b, align 4
  %2 = select i1 %1, i32 25, i32 0
  store i1 true, ptr @b, align 4
  store i8 0, ptr @am, align 1, !tbaa !5
  br label %3

3: ; preds = %22, %0
  %4 = phi i8 [ %24, %22 ], [ 0, %0 ]
  %5 = phi i32 [ -3846, %22 ], [ %2, %0 ]
  %6 = icmp eq i32 %5, 0
  %7 = sub nsw i32 %5, %2
  br i1 %6, label %10, label %8

8: ; preds = %3
  %9 = icmp slt i32 %7, 1
  br label %17

10:                                               ; preds = %3
  %11 = icmp slt i32 %7, 1
  br i1 %11, label %22, label %12

12:                                               ; preds = %10, %12
  %13 = phi i8 [ %14, %12 ], [ %4, %10 ]
  %14 = add nsw i8 %13, -6
  %15 = icmp ult i8 %13, -122
  br i1 %15, label %12, label %16, !llvm.loop !8

16:                                               ; preds = %12
  store i8 %14, ptr @am, align 1, !tbaa !5
  tail call void @bar149_() #2
  br label %26

17: ; preds = %8, %19
  tail call void @bar155_() #2
  %18 = load i8, ptr @am, align 1, !tbaa !5
  br i1 %9, label %22, label %19

19:                                               ; preds = %17
  %20 = add i8 %18, -6
  store i8 %20, ptr @am, align 1, !tbaa !5
  %21 = icmp ult i8 %18, -122
  br i1 %21, label %17, label %26, !llvm.loop !8

22:                                               ; preds = %17, %10
  %23 = phi i8 [ %4, %10 ], [ %18, %17 ]
  store i1 true, ptr @c, align 2
  %24 = add i8 %23, -6
  store i8 %24, ptr @am, align 1, !tbaa !5
  %25 = icmp ult i8 %23, -122
  br i1 %25, label %3, label %29, !llvm.loop !8

26: ; preds = %19, %16
  %27 = load i1, ptr @c, align 2
  br i1 %27, label %29, label %28

28: ; preds = %26
  tail call void @foo() #2
  br label %29

29: ; preds = %22, %28, %26
  ret i32 0
}

declare void @foo() local_unnamed_addr #1

declare void @bar155_() local_unnamed_addr #1

declare void @bar149_() local_unnamed_addr #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+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 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 18.0.0 ([email protected]:llvm/llvm-project.git 87a8e22475364b79d43515159de1b7947554d868)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
```


</p></details>

-----------------------------------------------------------------------

`clang-af2ed9f794c422923414d162dd1f48d986e2d6e3 -O3 case.c -S -emit-llvm -o case.ll`
<details ><summary>Emitted IR</summary><p>

```ll
; 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 = internal unnamed_addr global i1 false, align 4
@c = internal unnamed_addr global i1 false, align 2
@am = internal unnamed_addr global i8 0, align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  %1 = load i1, ptr @b, align 4
  store i1 true, ptr @b, align 4
 store i8 0, ptr @am, align 1, !tbaa !5
  br i1 %1, label %2, label %4

2:                                                ; preds = %0
 tail call void @bar155_() #2
  %3 = load i8, ptr @am, align 1, !tbaa !5
  br label %4

4: ; preds = %2, %0
  %5 = phi i8 [ %3, %2 ], [ 0, %0 ]
  store i1 true, ptr @c, align 2
  %6 = add i8 %5, -6
  store i8 %6, ptr @am, align 1, !tbaa !5
  %7 = icmp ult i8 %5, -122
  br i1 %7, label %8, label %13

8:                                                ; preds = %4, %8
  %9 = phi i8 [ %11, %8 ], [ %6, %4 ]
  tail call void @bar155_() #2
  %10 = load i8, ptr @am, align 1, !tbaa !5
  store i1 true, ptr @c, align 2
  %11 = add i8 %10, -6
  store i8 %11, ptr @am, align 1, !tbaa !5
  %12 = icmp ult i8 %10, -122
  br i1 %12, label %8, label %13, !llvm.loop !8

13:                                               ; preds = %8, %4
  ret i32 0
}

declare void @bar155_() local_unnamed_addr #1

attributes #0 = { nounwind uwtable "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 = { "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 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 16.0.6 ([email protected]:llvm/llvm-project.git 7cbf1a2591520c2491aa35339f227775f4d3adf6)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9, !10}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.peeled.count", i32 1}
```


</p></details>

-----------------------------------------------------------------------

Bisects to 0d95b20b63d7acc459dc0b2a7b2e4f9924c0adce
Committed by: @xortator

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to