; LLVM IR from nbdd0121/GenerationalGC :: MemorySpace.ll (ZN6norlit2gc11MemorySpace4TrimEm)

; 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.norlit::gc::MemorySpace" = type { i64, i64, i64, ptr, [1 x i64] }

; 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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN6norlit2gc11MemorySpace3EndEv(ptr noundef nonnull align 8 dereferenceable(40)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN6norlit2gc11MemorySpace7DestroyEv(ptr noundef nonnull align 8 dereferenceable(40)) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6norlit2gc11MemorySpace4TrimEm(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1) #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %50

11:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  %12 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = call noundef ptr @_ZN6norlit2gc11MemorySpace5BeginEv(ptr noundef nonnull align 8 dereferenceable(40) %13)
  %15 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = call noundef ptr @_ZN6norlit2gc11MemorySpace3EndEv(ptr noundef nonnull align 8 dereferenceable(40) %16)
  %18 = icmp eq ptr %14, %17
  %19 = zext i1 %18 to i8
  store i8 %19, ptr %5, align 1, !tbaa !13
  %20 = load i8, ptr %5, align 1, !tbaa !13, !range !15, !noundef !16
  %21 = trunc i8 %20 to i1
  br i1 %21, label %22, label %45

22:                                               ; preds = %11
  %23 = load i64, ptr %4, align 8, !tbaa !9
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %30

25:                                               ; preds = %22
  %26 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  %28 = load i64, ptr %4, align 8, !tbaa !9
  %29 = sub i64 %28, 1
  call void @_ZN6norlit2gc11MemorySpace4TrimEm(ptr noundef nonnull align 8 dereferenceable(40) %27, i64 noundef %29)
  br label %44

30:                                               ; preds = %22
  %31 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  call void @_ZN6norlit2gc11MemorySpace4TrimEm(ptr noundef nonnull align 8 dereferenceable(40) %32, i64 noundef 0)
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %33 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  %34 = load ptr, ptr %33, align 8, !tbaa !11
  %35 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %34, i32 0, i32 3
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  store ptr %36, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  %39 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %38, i32 0, i32 3
  store ptr null, ptr %39, align 8, !tbaa !11
  %40 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  call void @_ZN6norlit2gc11MemorySpace7DestroyEv(ptr noundef nonnull align 8 dereferenceable(40) %41)
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  store ptr %42, ptr %43, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %44

44:                                               ; preds = %30, %25
  br label %49

45:                                               ; preds = %11
  %46 = getelementptr inbounds %"struct.norlit::gc::MemorySpace", ptr %7, i32 0, i32 3
  %47 = load ptr, ptr %46, align 8, !tbaa !11
  %48 = load i64, ptr %4, align 8, !tbaa !9
  call void @_ZN6norlit2gc11MemorySpace4TrimEm(ptr noundef nonnull align 8 dereferenceable(40) %47, i64 noundef %48)
  br label %49

49:                                               ; preds = %45, %44
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  br label %50

50:                                               ; preds = %49, %2
  ret void
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN6norlit2gc11MemorySpace5BeginEv(ptr noundef nonnull align 8 dereferenceable(40)) #1 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 #2 = { 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 #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 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 24}
!12 = !{!"_ZTSN6norlit2gc11MemorySpaceE", !10, i64 0, !10, i64 8, !10, i64 16, !6, i64 24, !7, i64 32}
!13 = !{!14, !14, i64 0}
!14 = !{!"bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}


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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN6norlit2gc11MemorySpace3EndEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN6norlit2gc11MemorySpace7DestroyEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6norlit2gc11MemorySpace4TrimEm(ptr nocapture noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1) local_unnamed_addr #1 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 24
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %.not910 = icmp eq ptr %4, null
  br i1 %.not910, label %common.ret30, label %.lr.ph

.lr.ph:                                           ; preds = %2, %tailrecurse.outer
  %5 = phi ptr [ %18, %tailrecurse.outer ], [ %4, %2 ]
  %6 = phi ptr [ %17, %tailrecurse.outer ], [ %3, %2 ]
  %.tr7.ph11 = phi i64 [ %16, %tailrecurse.outer ], [ %1, %2 ]
  br label %7

7:                                                ; preds = %.lr.ph, %tailrecurse
  %8 = phi ptr [ %5, %.lr.ph ], [ %26, %tailrecurse ]
  %9 = phi ptr [ %6, %.lr.ph ], [ %25, %tailrecurse ]
  %10 = tail call noundef ptr @_ZN6norlit2gc11MemorySpace5BeginEv(ptr noundef nonnull align 8 dereferenceable(40) %8)
  %11 = load ptr, ptr %9, align 8, !tbaa !5
  %12 = tail call noundef ptr @_ZN6norlit2gc11MemorySpace3EndEv(ptr noundef nonnull align 8 dereferenceable(40) %11)
  %13 = icmp eq ptr %10, %12
  br i1 %13, label %14, label %tailrecurse

14:                                               ; preds = %7
  %.not6 = icmp eq i64 %.tr7.ph11, 0
  %15 = load ptr, ptr %9, align 8, !tbaa !5
  br i1 %.not6, label %19, label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %14
  %16 = add i64 %.tr7.ph11, -1
  %17 = getelementptr inbounds i8, ptr %15, i64 24
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  %.not9 = icmp eq ptr %18, null
  br i1 %.not9, label %common.ret30, label %.lr.ph

common.ret30:                                     ; preds = %2, %tailrecurse, %tailrecurse.outer, %19
  ret void

19:                                               ; preds = %14
  tail call void @_ZN6norlit2gc11MemorySpace4TrimEm(ptr noundef nonnull align 8 dereferenceable(40) %15, i64 noundef 0)
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 24
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  store ptr null, ptr %21, align 8, !tbaa !5
  %23 = load ptr, ptr %9, align 8, !tbaa !5
  tail call void @_ZN6norlit2gc11MemorySpace7DestroyEv(ptr noundef nonnull align 8 dereferenceable(40) %23)
  store ptr %22, ptr %9, align 8, !tbaa !5
  br label %common.ret30

tailrecurse:                                      ; preds = %7
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i64 24
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  %.not = icmp eq ptr %26, null
  br i1 %.not, label %common.ret30, label %7
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN6norlit2gc11MemorySpace5BeginEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #0 align 2

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

!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, !10, i64 24}
!6 = !{!"_ZTSN6norlit2gc11MemorySpaceE", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !8, i64 32}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}


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