; LLVM IR from aras-p/ToyPathTracer :: Cpp/Source/enkiTS/TaskScheduler.ll (ZN4enki13TaskScheduler9HaveTasksEj)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%"class.enki::TaskScheduler" = type <{ [3 x ptr], [3 x ptr], i32, [4 x i8], ptr, ptr, %"struct.std::atomic", %"struct.std::atomic", %"struct.std::atomic", i8, %"struct.std::atomic.0", %"struct.std::atomic.0", %"struct.std::atomic.0", i32, [4 x i8], ptr, ptr, i32, i8, [3 x i8], %"struct.enki::TaskSchedulerConfig", %"struct.std::atomic.0", [4 x i8] }>
%"struct.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i8 }
%"struct.enki::TaskSchedulerConfig" = type { i32, i32, %"struct.enki::ProfilerCallbacks", %"struct.enki::CustomAllocator" }
%"struct.enki::ProfilerCallbacks" = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%"struct.enki::CustomAllocator" = type { ptr, ptr, ptr }
%"struct.std::atomic.0" = type { %"struct.std::__atomic_base.1" }
%"struct.std::__atomic_base.1" = type { i32 }
%"class.enki::TaskPipe" = type { %"class.enki::LockLessMultiReadPipe.base", [4 x i8] }
%"class.enki::LockLessMultiReadPipe.base" = type <{ [256 x %"struct.enki::SubTaskSet"], %"struct.std::atomic.3", %"struct.std::atomic.3", [256 x %"struct.std::atomic.3"], %"struct.std::atomic.3" }>
%"struct.enki::SubTaskSet" = type { ptr, %"struct.enki::TaskSetPartition" }
%"struct.enki::TaskSetPartition" = type { i32, i32 }
%"struct.std::atomic.3" = type { %"struct.std::__atomic_base.4" }
%"struct.std::__atomic_base.4" = type { i32 }
%"class.enki::PinnedTaskList" = type { %"class.enki::LocklessMultiWriteIntrusiveList" }
%"class.enki::LocklessMultiWriteIntrusiveList" = type { %"struct.std::atomic.5", %"class.enki::IPinnedTask" }
%"struct.std::atomic.5" = type { %"struct.std::__atomic_base.6" }
%"struct.std::__atomic_base.6" = type { ptr }
%"class.enki::IPinnedTask" = type { %"class.enki::ICompletable", i32, %"struct.std::atomic.5" }
%"class.enki::ICompletable" = type { ptr, i32, %"struct.std::atomic.0", %"struct.std::atomic.0", i32, %"struct.std::atomic.0", ptr }

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN4enki13TaskScheduler9HaveTasksEj(ptr noundef nonnull align 8 dereferenceable(220) %0, i32 noundef %1) #1 align 2 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %9 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %50, %2
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = icmp slt i32 %11, 3
  br i1 %12, label %14, label %13

13:                                               ; preds = %10
  store i32 2, ptr %7, align 4
  br label %53

14:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %33, %14
  %16 = load i32, ptr %8, align 4, !tbaa !9
  %17 = getelementptr inbounds %"class.enki::TaskScheduler", ptr %9, i32 0, i32 2
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = icmp ult i32 %16, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %15
  store i32 5, ptr %7, align 4
  br label %36

21:                                               ; preds = %15
  %22 = getelementptr inbounds %"class.enki::TaskScheduler", ptr %9, i32 0, i32 0
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [3 x ptr], ptr %22, i64 0, i64 %24
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds %"class.enki::TaskPipe", ptr %26, i64 %28
  %30 = call noundef zeroext i1 @_ZNK4enki21LockLessMultiReadPipeILh8ENS_10SubTaskSetEE11IsPipeEmptyEv(ptr noundef nonnull align 8 dereferenceable(5132) %29)
  br i1 %30, label %32, label %31

31:                                               ; preds = %21
  store i1 true, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %36

32:                                               ; preds = %21
  br label %33

33:                                               ; preds = %32
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = add i32 %34, 1
  store i32 %35, ptr %8, align 4, !tbaa !9
  br label %15, !llvm.loop !21

36:                                               ; preds = %31, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %37 = load i32, ptr %7, align 4
  switch i32 %37, label %53 [
    i32 5, label %38
  ]

38:                                               ; preds = %36
  %39 = getelementptr inbounds %"class.enki::TaskScheduler", ptr %9, i32 0, i32 1
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [3 x ptr], ptr %39, i64 0, i64 %41
  %43 = load ptr, ptr %42, align 8, !tbaa !5
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = zext i32 %44 to i64
  %46 = getelementptr inbounds %"class.enki::PinnedTaskList", ptr %43, i64 %45
  %47 = call noundef zeroext i1 @_ZNK4enki31LocklessMultiWriteIntrusiveListINS_11IPinnedTaskEE11IsListEmptyEv(ptr noundef nonnull align 8 dereferenceable(64) %46)
  br i1 %47, label %49, label %48

48:                                               ; preds = %38
  store i1 true, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %53

49:                                               ; preds = %38
  br label %50

50:                                               ; preds = %49
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %6, align 4, !tbaa !9
  br label %10, !llvm.loop !23

53:                                               ; preds = %48, %36, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %54 = load i32, ptr %7, align 4
  switch i32 %54, label %58 [
    i32 2, label %55
    i32 1, label %56
  ]

55:                                               ; preds = %53
  store i1 false, ptr %3, align 1
  br label %56

56:                                               ; preds = %55, %53
  %57 = load i1, ptr %3, align 1
  ret i1 %57

58:                                               ; preds = %53
  unreachable
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK4enki21LockLessMultiReadPipeILh8ENS_10SubTaskSetEE11IsPipeEmptyEv(ptr noundef nonnull align 8 dereferenceable(5132)) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK4enki31LocklessMultiWriteIntrusiveListINS_11IPinnedTaskEE11IsListEmptyEv(ptr noundef nonnull align 8 dereferenceable(64)) #2 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 #2 = { mustprogress 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 #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 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 48}
!12 = !{!"_ZTSN4enki13TaskSchedulerE", !7, i64 0, !7, i64 24, !10, i64 48, !6, i64 56, !6, i64 64, !13, i64 72, !13, i64 73, !13, i64 74, !16, i64 76, !16, i64 80, !16, i64 84, !10, i64 88, !6, i64 96, !6, i64 104, !10, i64 112, !15, i64 116, !18, i64 120, !16, i64 216}
!13 = !{!"_ZTSSt6atomicIbE", !14, i64 0}
!14 = !{!"_ZTSSt13__atomic_baseIbE", !15, i64 0}
!15 = !{!"bool", !7, i64 0}
!16 = !{!"_ZTSSt6atomicIiE", !17, i64 0}
!17 = !{!"_ZTSSt13__atomic_baseIiE", !10, i64 0}
!18 = !{!"_ZTSN4enki19TaskSchedulerConfigE", !10, i64 0, !10, i64 4, !19, i64 8, !20, i64 72}
!19 = !{!"_ZTSN4enki17ProfilerCallbacksE", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!20 = !{!"_ZTSN4enki15CustomAllocatorE", !6, i64 0, !6, i64 8, !6, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%"class.enki::TaskPipe" = type { %"class.enki::LockLessMultiReadPipe.base", [4 x i8] }
%"class.enki::LockLessMultiReadPipe.base" = type <{ [256 x %"struct.enki::SubTaskSet"], %"struct.std::atomic.3", %"struct.std::atomic.3", [256 x %"struct.std::atomic.3"], %"struct.std::atomic.3" }>
%"struct.enki::SubTaskSet" = type { ptr, %"struct.enki::TaskSetPartition" }
%"struct.enki::TaskSetPartition" = type { i32, i32 }
%"struct.std::atomic.3" = type { %"struct.std::__atomic_base.4" }
%"struct.std::__atomic_base.4" = type { i32 }
%"class.enki::PinnedTaskList" = type { %"class.enki::LocklessMultiWriteIntrusiveList" }
%"class.enki::LocklessMultiWriteIntrusiveList" = type { %"struct.std::atomic.5", %"class.enki::IPinnedTask" }
%"struct.std::atomic.5" = type { %"struct.std::__atomic_base.6" }
%"struct.std::__atomic_base.6" = type { ptr }
%"class.enki::IPinnedTask" = type { %"class.enki::ICompletable", i32, %"struct.std::atomic.5" }
%"class.enki::ICompletable" = type { ptr, i32, %"struct.std::atomic.0", %"struct.std::atomic.0", i32, %"struct.std::atomic.0", ptr }
%"struct.std::atomic.0" = type { %"struct.std::__atomic_base.1" }
%"struct.std::__atomic_base.1" = type { i32 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN4enki13TaskScheduler9HaveTasksEj(ptr nocapture noundef nonnull readonly align 8 dereferenceable(220) %0, i32 noundef %1) local_unnamed_addr #0 align 2 {
.preheader:
  %2 = getelementptr inbounds i8, ptr %0, i64 48
  %3 = getelementptr inbounds i8, ptr %0, i64 24
  %4 = zext i32 %1 to i64
  %5 = load i32, ptr %2, align 8, !tbaa !5
  %.not18.not = icmp eq i32 %5, 0
  br i1 %.not18.not, label %._crit_edge, label %.lr.ph

.preheader.1:                                     ; preds = %._crit_edge
  %6 = load i32, ptr %2, align 8, !tbaa !5
  %.not18.not.1 = icmp eq i32 %6, 0
  br i1 %.not18.not.1, label %._crit_edge.1, label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.preheader.1
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  br label %8

8:                                                ; preds = %12, %.lr.ph.1
  %indvars.iv.1 = phi i64 [ 0, %.lr.ph.1 ], [ %indvars.iv.next.1, %12 ]
  %9 = load ptr, ptr %7, align 8, !tbaa !19
  %10 = getelementptr inbounds %"class.enki::TaskPipe", ptr %9, i64 %indvars.iv.1
  %11 = tail call noundef zeroext i1 @_ZNK4enki21LockLessMultiReadPipeILh8ENS_10SubTaskSetEE11IsPipeEmptyEv(ptr noundef nonnull align 8 dereferenceable(5132) %10)
  br i1 %11, label %12, label %.thread

12:                                               ; preds = %8
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv.1, 1
  %13 = load i32, ptr %2, align 8, !tbaa !5
  %14 = zext i32 %13 to i64
  %.not.1 = icmp ult i64 %indvars.iv.next.1, %14
  br i1 %.not.1, label %8, label %._crit_edge.1, !llvm.loop !20

._crit_edge.1:                                    ; preds = %12, %.preheader.1
  %15 = getelementptr inbounds i8, ptr %0, i64 32
  %16 = load ptr, ptr %15, align 8, !tbaa !19
  %17 = getelementptr inbounds %"class.enki::PinnedTaskList", ptr %16, i64 %4
  %18 = tail call noundef zeroext i1 @_ZNK4enki31LocklessMultiWriteIntrusiveListINS_11IPinnedTaskEE11IsListEmptyEv(ptr noundef nonnull align 8 dereferenceable(64) %17)
  br i1 %18, label %.preheader.2, label %.thread

.preheader.2:                                     ; preds = %._crit_edge.1
  %19 = load i32, ptr %2, align 8, !tbaa !5
  %.not18.not.2 = icmp eq i32 %19, 0
  br i1 %.not18.not.2, label %._crit_edge.2, label %.lr.ph.2

.lr.ph.2:                                         ; preds = %.preheader.2
  %20 = getelementptr inbounds i8, ptr %0, i64 16
  br label %21

21:                                               ; preds = %25, %.lr.ph.2
  %indvars.iv.2 = phi i64 [ 0, %.lr.ph.2 ], [ %indvars.iv.next.2, %25 ]
  %22 = load ptr, ptr %20, align 8, !tbaa !19
  %23 = getelementptr inbounds %"class.enki::TaskPipe", ptr %22, i64 %indvars.iv.2
  %24 = tail call noundef zeroext i1 @_ZNK4enki21LockLessMultiReadPipeILh8ENS_10SubTaskSetEE11IsPipeEmptyEv(ptr noundef nonnull align 8 dereferenceable(5132) %23)
  br i1 %24, label %25, label %.thread

25:                                               ; preds = %21
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv.2, 1
  %26 = load i32, ptr %2, align 8, !tbaa !5
  %27 = zext i32 %26 to i64
  %.not.2 = icmp ult i64 %indvars.iv.next.2, %27
  br i1 %.not.2, label %21, label %._crit_edge.2, !llvm.loop !20

._crit_edge.2:                                    ; preds = %25, %.preheader.2
  %28 = getelementptr inbounds i8, ptr %0, i64 40
  %29 = load ptr, ptr %28, align 8, !tbaa !19
  %30 = getelementptr inbounds %"class.enki::PinnedTaskList", ptr %29, i64 %4
  %31 = tail call noundef zeroext i1 @_ZNK4enki31LocklessMultiWriteIntrusiveListINS_11IPinnedTaskEE11IsListEmptyEv(ptr noundef nonnull align 8 dereferenceable(64) %30)
  %not. = xor i1 %31, true
  br label %.thread

32:                                               ; preds = %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %33 = load i32, ptr %2, align 8, !tbaa !5
  %34 = zext i32 %33 to i64
  %.not = icmp ult i64 %indvars.iv.next, %34
  br i1 %.not, label %.lr.ph, label %._crit_edge, !llvm.loop !20

.lr.ph:                                           ; preds = %.preheader, %32
  %indvars.iv = phi i64 [ %indvars.iv.next, %32 ], [ 0, %.preheader ]
  %35 = load ptr, ptr %0, align 8, !tbaa !19
  %36 = getelementptr inbounds %"class.enki::TaskPipe", ptr %35, i64 %indvars.iv
  %37 = tail call noundef zeroext i1 @_ZNK4enki21LockLessMultiReadPipeILh8ENS_10SubTaskSetEE11IsPipeEmptyEv(ptr noundef nonnull align 8 dereferenceable(5132) %36)
  br i1 %37, label %32, label %.thread

._crit_edge:                                      ; preds = %32, %.preheader
  %38 = load ptr, ptr %3, align 8, !tbaa !19
  %39 = getelementptr inbounds %"class.enki::PinnedTaskList", ptr %38, i64 %4
  %40 = tail call noundef zeroext i1 @_ZNK4enki31LocklessMultiWriteIntrusiveListINS_11IPinnedTaskEE11IsListEmptyEv(ptr noundef nonnull align 8 dereferenceable(64) %39)
  br i1 %40, label %.preheader.1, label %.thread

.thread:                                          ; preds = %.lr.ph, %8, %21, %._crit_edge.2, %._crit_edge, %._crit_edge.1
  %41 = phi i1 [ true, %._crit_edge ], [ true, %._crit_edge.1 ], [ %not., %._crit_edge.2 ], [ true, %21 ], [ true, %8 ], [ true, %.lr.ph ]
  ret i1 %41
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK4enki21LockLessMultiReadPipeILh8ENS_10SubTaskSetEE11IsPipeEmptyEv(ptr noundef nonnull align 8 dereferenceable(5132)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK4enki31LocklessMultiWriteIntrusiveListINS_11IPinnedTaskEE11IsListEmptyEv(ptr noundef nonnull align 8 dereferenceable(64)) local_unnamed_addr #0 align 2

attributes #0 = { mustprogress 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" }

!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 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !9, i64 48}
!6 = !{!"_ZTSN4enki13TaskSchedulerE", !7, i64 0, !7, i64 24, !9, i64 48, !10, i64 56, !10, i64 64, !11, i64 72, !11, i64 73, !11, i64 74, !14, i64 76, !14, i64 80, !14, i64 84, !9, i64 88, !10, i64 96, !10, i64 104, !9, i64 112, !13, i64 116, !16, i64 120, !14, i64 216}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!"_ZTSSt6atomicIbE", !12, i64 0}
!12 = !{!"_ZTSSt13__atomic_baseIbE", !13, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = !{!"_ZTSSt6atomicIiE", !15, i64 0}
!15 = !{!"_ZTSSt13__atomic_baseIiE", !9, i64 0}
!16 = !{!"_ZTSN4enki19TaskSchedulerConfigE", !9, i64 0, !9, i64 4, !17, i64 8, !18, i64 72}
!17 = !{!"_ZTSN4enki17ProfilerCallbacksE", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56}
!18 = !{!"_ZTSN4enki15CustomAllocatorE", !10, i64 0, !10, i64 8, !10, i64 16}
!19 = !{!10, !10, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass