Issue 64640
Summary Dead Code Elimination Regression at -O2 (trunk vs. llvmorg-16.0.6)
Labels new issue
Assignees
Reporter SvenjaScherrer
    ```c
static int b;
static char c = 4;
static unsigned char e;
static short f;
void bar88_(void);
void foo(void);
static char(a)(unsigned char g, char i) { return i == 0 ?: g / i; }
static short(d)(short g, short i) { return i == 0 ?: g % i; }
static void h() {
  int j = 21969957;
  for (; c; --c) {
    bar88_();
    e = a(~0, b);
    f = d(1, e);
    j |= f;
    if (!(j <= 21969957))
      foo();
    b = 0;
 }
}
int main() {
  b = d(4, 9);
 h();
}
```

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

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

`clang-f36e909da03767c317eaa5ded9abbf3214299ff5 -O2 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 i8 4, align 1

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

3:                                                ; preds = %0, %11
  %4 = phi i32 [ %8, %11 ], [ 21969957, %0 ]
  tail call void @bar88_() #2
  %5 = load i1, ptr @b, align 4
  %6 = freeze i1 %5
  %7 = zext i1 %6 to i32
  %8 = or i32 %4, %7
  %9 = icmp slt i32 %8, 21969958
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %10, %3
  store i1 false, ptr @b, align 4
  %12 = load i8, ptr @c, align 1, !tbaa !5
  %13 = add i8 %12, -1
  store i8 %13, ptr @c, align 1, !tbaa !5
  %14 = icmp eq i8 %13, 0
  br i1 %14, label %15, label %3, !llvm.loop !8

15:                                               ; preds = %11, %0
  ret i32 0
}

declare void @bar88_() local_unnamed_addr #1

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 f36e909da03767c317eaa5ded9abbf3214299ff5)"}
!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 -O2 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"

@c = internal unnamed_addr global i8 4, align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  %1 = load i8, ptr @c, align 1, !tbaa !5
  %2 = icmp eq i8 %1, 0
  br i1 %2, label %7, label %3

3:                                                ; preds = %0, %3
  tail call void @bar88_() #2
  %4 = load i8, ptr @c, align 1, !tbaa !5
  %5 = add i8 %4, -1
  store i8 %5, ptr @c, align 1, !tbaa !5
 %6 = icmp eq i8 %5, 0
  br i1 %6, label %7, label %3, !llvm.loop !8

7:                                                ; preds = %3, %0
  ret i32 0
}

declare void @bar88_() 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}
!9 = !{!"llvm.loop.mustprogress"}
```


</p></details>

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

Bisects to d8e9dd33b2a5546af0b26243810712629d4d5f14
Committed by: @goldsteinn

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

Reply via email to