Issue 64641
Summary Dead Code Elimination Regression at -O3 (trunk vs. llvmorg-16.0.6)
Labels new issue
Assignees
Reporter SvenjaScherrer
    ```c
static int a, b, c;
static int *e = &a, *f = &a;
void bar25_(void);
void foo(void);
static void d() { *f = 1; }
int main() {
  {
    int g = 0;
    char h;
 d();
    b = b - 3;
    for (; b;) {
      if (g)
 foo();
      int *i[] = {&g, &g};
      h = 1;
      for (; h >= 0; h--) {
        if (h)
          bar25_();
        c |= i[h] != 0;
        if (*e)
          break;
      }
    }
 }
}
```

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

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

`clang-f36e909da03767c317eaa5ded9abbf3214299ff5 -O3 case.c -S -emit-llvm -o case.suc20p.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 i32 0, align 4
@c = internal unnamed_addr global i32 0, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  %2 = load i32, ptr @b, align 4, !tbaa !5
  %3 = add nsw i32 %2, -3
  store i32 %3, ptr @b, align 4, !tbaa !5
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %15, label %5

5: ; preds = %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = icmp eq i32 %6, 0
  br label %8

8: ; preds = %5, %10
  br i1 %7, label %10, label %9

9:                                                ; preds = %8
  tail call void @foo() #3
  br label %10

10: ; preds = %9, %8
  tail call void @bar25_() #3
  %11 = load i32, ptr @c, align 4, !tbaa !5
  %12 = or i32 %11, 1
  store i32 %12, ptr @c, align 4, !tbaa !5
  %13 = load i32, ptr @b, align 4, !tbaa !5
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %8, !llvm.loop !9

15: ; preds = %10, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret i32 0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare void @foo() local_unnamed_addr #2

declare void @bar25_() local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 #3 = { 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 f36e909da03767c317eaa5ded9abbf3214299ff5)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
```


</p></details>

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

`clang-af2ed9f794c422923414d162dd1f48d986e2d6e3 -O3 case.c -S -emit-llvm -o case.suc20p.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 i32 0, align 4
@c = internal unnamed_addr global i32 0, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  %1 = load i32, ptr @b, align 4, !tbaa !5
  %2 = add nsw i32 %1, -3
  store i32 %2, ptr @b, align 4, !tbaa !5
  %3 = icmp eq i32 %2, 0
  br i1 %3, label %9, label %4

4: ; preds = %0, %4
  tail call void @bar25_() #2
  %5 = load i32, ptr @c, align 4, !tbaa !5
  %6 = or i32 %5, 1
 store i32 %6, ptr @c, align 4, !tbaa !5
  %7 = load i32, ptr @b, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %4, !llvm.loop !9

9:                                                ; preds = %4, %0
  ret i32 0
}

declare void @bar25_() 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
```


</p></details>

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

Bisects to 849d01bf3d97d05074a00d78903d782b2ac804f8
Committed by: @caojoshua
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to