Issue 64678
Summary Dead Code Elimination Regression at -O3 (trunk vs. llvmorg-16.0.6)
Labels new issue
Assignees
Reporter SvenjaScherrer
    ```c
static int b, c;
static int *e = &b, *f = &b;
static short h = 64161;
static unsigned i;
static int **j = &e;
void foo(void);
static int(a)(int k, int m) { return m == 0 ? 0 : k % m; }
static short(d)(short k, short m) { return k + m; }
static long(g)(long k, long m) { return m == 0 ?: k % m; }
static int *l(long k, int *m) {
  if (k)
    h = 0;
  if (*e) {
    h = 8;
    if (!k)
      foo();
    *f = d(k, k);
    if (g(0 == k | k && k, b))
      for (;;)
        ;
  }
  return m;
}
int main() {
  i = b;
  c = a(0 || h, h);
  *j = l((i < c | h) == h || h, *j);
}
```

`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 i32 0, align 4
@h = internal unnamed_addr global i16 -1375, align 2

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  %1 = load i32, ptr @b, align 4, !tbaa !5
  %2 = load i16, ptr @h, align 2
  %3 = sext i16 %2 to i32
  %4 = icmp ne i16 %2, 0
  %5 = icmp eq i16 %2, 0
  br i1 %5, label %9, label %6

6:                                                ; preds = %0
 %7 = srem i16 1, %2
  %8 = zext i16 %7 to i32
  br label %9

9: ; preds = %0, %6
  %10 = phi i32 [ %8, %6 ], [ 0, %0 ]
  %11 = icmp ult i32 %1, %10
  %12 = zext i1 %11 to i32
  %13 = or i32 %12, %3
  %14 = icmp eq i32 %13, %3
  %15 = or i1 %4, %14
  %16 = zext i1 %15 to i64
  br i1 %15, label %17, label %20

17:                                               ; preds = %9
  store i16 0, ptr @h, align 2, !tbaa !9
  %18 = load i32, ptr @b, align 4, !tbaa !5
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %34, label %23

20:                                               ; preds = %9
  %21 = load i32, ptr @b, align 4, !tbaa !5
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %34, label %24

23: ; preds = %17
  store i16 8, ptr @h, align 2, !tbaa !9
  br label %25

24: ; preds = %20
  store i16 8, ptr @h, align 2, !tbaa !9
  tail call void @foo() #2
  br label %25

25: ; preds = %24, %23
  %26 = phi i16 [ 0, %24 ], [ 2, %23 ]
  %27 = zext i16 %26 to i32
  store i32 %27, ptr @b, align 4, !tbaa !5
  %28 = zext i16 %26 to i64
  %29 = icmp ult i64 %16, %28
  %30 = select i1 %29, i64 0, i64 %28
  %31 = icmp eq i64 %30, %16
  %32 = and i1 %15, %31
  br i1 %32, label %34, label %33

33: ; preds = %25, %33
  br label %33

34: ; preds = %17, %20, %25
  ret i32 0
}

declare void @foo() 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 = !{!"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}
```


</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 global i32 0, align 4
@h = internal unnamed_addr global i16 -1375, align 2
@e = internal unnamed_addr global ptr @b, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
 %1 = load ptr, ptr @e, align 8, !tbaa !5
  store i16 0, ptr @h, align 2, !tbaa !9
  %2 = load i32, ptr %1, align 4, !tbaa !11
  %3 = icmp eq i32 %2, 0
  br i1 %3, label %6, label %4

4: ; preds = %0
  store i16 8, ptr @h, align 2, !tbaa !9
  store i32 2, ptr @b, align 4, !tbaa !11
  br label %5

5: ; preds = %4, %5
  br label %5

6:                                                ; preds = %0
 store ptr %1, ptr @e, align 8, !tbaa !5
  ret i32 0
}

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" }

!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 = !{!"any pointer", !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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
```


</p></details>

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

Bisects to b80f31c48d63743ece1062f30d5f747a03ee5695
Committed by: @vfdff

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

Reply via email to