; LLVM IR from brewlin/garbage-collect :: mark-sweep-multi-free-list (mark_sweep)

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

%struct.gc_heap = type { ptr, i64 }
%struct.header = type { i64, i64, ptr }

@gc_heaps_used = external global i64, align 8
@gc_heaps = external global [1000000 x %struct.gc_heap], align 16

; 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: nounwind uwtable
define dso_local void @gc_sweep() #1 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %61, %0
  %6 = load i64, ptr %1, align 8, !tbaa !5
  %7 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %8 = icmp ult i64 %6, %7
  br i1 %8, label %9, label %64

9:                                                ; preds = %5
  %10 = load i64, ptr %1, align 8, !tbaa !5
  %11 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %10
  %12 = getelementptr inbounds %struct.gc_heap, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 16, !tbaa !9
  %14 = ptrtoint ptr %13 to i64
  %15 = load i64, ptr %1, align 8, !tbaa !5
  %16 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.gc_heap, ptr %16, i32 0, i32 1
  %18 = load i64, ptr %17, align 8, !tbaa !12
  %19 = add i64 %14, %18
  %20 = inttoptr i64 %19 to ptr
  store ptr %20, ptr %3, align 8, !tbaa !13
  %21 = load i64, ptr %1, align 8, !tbaa !5
  %22 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.gc_heap, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 16, !tbaa !9
  store ptr %24, ptr %2, align 8, !tbaa !13
  br label %25

25:                                               ; preds = %51, %9
  %26 = load ptr, ptr %2, align 8, !tbaa !13
  %27 = load ptr, ptr %3, align 8, !tbaa !13
  %28 = icmp ult ptr %26, %27
  br i1 %28, label %29, label %60

29:                                               ; preds = %25
  %30 = load ptr, ptr %2, align 8, !tbaa !13
  %31 = getelementptr inbounds %struct.header, ptr %30, i32 0, i32 0
  %32 = load i64, ptr %31, align 8, !tbaa !14
  %33 = and i64 %32, 1
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %50

35:                                               ; preds = %29
  %36 = load ptr, ptr %2, align 8, !tbaa !13
  %37 = getelementptr inbounds %struct.header, ptr %36, i32 0, i32 0
  %38 = load i64, ptr %37, align 8, !tbaa !14
  %39 = and i64 %38, 2
  %40 = icmp ne i64 %39, 0
  br i1 %40, label %41, label %46

41:                                               ; preds = %35
  %42 = load ptr, ptr %2, align 8, !tbaa !13
  %43 = getelementptr inbounds %struct.header, ptr %42, i32 0, i32 0
  %44 = load i64, ptr %43, align 8, !tbaa !14
  %45 = and i64 %44, -3
  store i64 %45, ptr %43, align 8, !tbaa !14
  br label %49

46:                                               ; preds = %35
  %47 = load ptr, ptr %2, align 8, !tbaa !13
  %48 = getelementptr inbounds %struct.header, ptr %47, i64 1
  call void @gc_free(ptr noundef %48)
  br label %49

49:                                               ; preds = %46, %41
  br label %50

50:                                               ; preds = %49, %29
  br label %51

51:                                               ; preds = %50
  %52 = load ptr, ptr %2, align 8, !tbaa !13
  %53 = getelementptr inbounds %struct.header, ptr %52, i64 1
  %54 = ptrtoint ptr %53 to i64
  %55 = load ptr, ptr %2, align 8, !tbaa !13
  %56 = getelementptr inbounds %struct.header, ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !16
  %58 = add i64 %54, %57
  %59 = inttoptr i64 %58 to ptr
  store ptr %59, ptr %2, align 8, !tbaa !13
  br label %25, !llvm.loop !17

60:                                               ; preds = %25
  br label %61

61:                                               ; preds = %60
  %62 = load i64, ptr %1, align 8, !tbaa !5
  %63 = add i64 %62, 1
  store i64 %63, ptr %1, align 8, !tbaa !5
  br label %5, !llvm.loop !19

64:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

declare void @gc_free(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"gc_heap", !11, i64 0, !6, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"header", !6, i64 0, !6, i64 8, !11, i64 16}
!16 = !{!15, !6, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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

%struct.gc_heap = type { ptr, i64 }

@gc_heaps_used = external local_unnamed_addr global i64, align 8
@gc_heaps = external local_unnamed_addr global [1000000 x %struct.gc_heap], align 16

; Function Attrs: nounwind uwtable
define dso_local void @gc_sweep() local_unnamed_addr #0 {
  %1 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %.not19 = icmp eq i64 %1, 0
  br i1 %.not19, label %._crit_edge18, label %.lr.ph17

.lr.ph17:                                         ; preds = %0, %._crit_edge
  %2 = phi i64 [ %27, %._crit_edge ], [ %1, %0 ]
  %.015 = phi i64 [ %28, %._crit_edge ], [ 0, %0 ]
  %3 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %.015
  %4 = load ptr, ptr %3, align 16, !tbaa !9
  %5 = ptrtoint ptr %4 to i64
  %6 = getelementptr inbounds i8, ptr %3, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !12
  %8 = add i64 %7, %5
  %9 = inttoptr i64 %8 to ptr
  %10 = icmp ult ptr %4, %9
  br i1 %10, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph17, %19
  %.01214 = phi ptr [ %25, %19 ], [ %4, %.lr.ph17 ]
  %11 = load i64, ptr %.01214, align 8, !tbaa !13
  %12 = and i64 %11, 1
  %.not = icmp eq i64 %12, 0
  br i1 %.not, label %19, label %13

13:                                               ; preds = %.lr.ph
  %14 = and i64 %11, 2
  %.not13 = icmp eq i64 %14, 0
  br i1 %.not13, label %17, label %15

15:                                               ; preds = %13
  %16 = and i64 %11, -3
  store i64 %16, ptr %.01214, align 8, !tbaa !13
  br label %19

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %.01214, i64 24
  tail call void @gc_free(ptr noundef nonnull %18) #2
  br label %19

19:                                               ; preds = %.lr.ph, %17, %15
  %20 = getelementptr inbounds i8, ptr %.01214, i64 24
  %21 = ptrtoint ptr %20 to i64
  %22 = getelementptr inbounds i8, ptr %.01214, i64 8
  %23 = load i64, ptr %22, align 8, !tbaa !15
  %24 = add i64 %23, %21
  %25 = inttoptr i64 %24 to ptr
  %26 = icmp ult ptr %25, %9
  br i1 %26, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !16

._crit_edge.loopexit:                             ; preds = %19
  %.pre = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.lr.ph17
  %27 = phi i64 [ %.pre, %._crit_edge.loopexit ], [ %2, %.lr.ph17 ]
  %28 = add nuw i64 %.015, 1
  %29 = icmp ult i64 %28, %27
  br i1 %29, label %.lr.ph17, label %._crit_edge18, !llvm.loop !18

._crit_edge18:                                    ; preds = %._crit_edge, %0
  ret void
}

declare void @gc_free(ptr noundef) 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 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"gc_heap", !11, i64 0, !6, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!14, !6, i64 0}
!14 = !{!"header", !6, i64 0, !6, i64 8, !11, i64 16}
!15 = !{!14, !6, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass