; LLVM IR from Deborah-Digges/mark-sweep-simulation :: 01-marksweep-simple/vm.ll (ZN2VM5sweepEv)

; 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.VM = type <{ %"class.std::deque", ptr, %"class.std::queue", i32, i32, i32, [4 x i8] }>
%"class.std::deque" = type { %"class.std::_Deque_base" }
%"class.std::_Deque_base" = type { %"struct.std::_Deque_base<Object *, std::allocator<Object *>>::_Deque_impl" }
%"struct.std::_Deque_base<Object *, std::allocator<Object *>>::_Deque_impl" = type { %"struct.std::_Deque_base<Object *, std::allocator<Object *>>::_Deque_impl_data" }
%"struct.std::_Deque_base<Object *, std::allocator<Object *>>::_Deque_impl_data" = type { ptr, i64, %"struct.std::_Deque_iterator", %"struct.std::_Deque_iterator" }
%"struct.std::_Deque_iterator" = type { ptr, ptr, ptr, ptr }
%"class.std::queue" = type { %"class.std::deque" }
%struct.Object = type { i32, i32, ptr, %union.anon }
%union.anon = type { %struct.anon }
%struct.anon = type { ptr, 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 void @_ZN2VM5sweepEv(ptr noundef nonnull align 8 dereferenceable(180) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %6 = getelementptr inbounds %class.VM, ptr %5, i32 0, i32 1
  store ptr %6, ptr %3, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %35, %1
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %36

11:                                               ; preds = %7
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Object, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %28, label %17

17:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  store ptr %19, ptr %4, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Object, ptr %20, i32 0, i32 2
  %22 = load ptr, ptr %21, align 8, !tbaa !13
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %22, ptr %23, align 8, !tbaa !5
  %24 = getelementptr inbounds %class.VM, ptr %5, i32 0, i32 2
  call void @_ZNSt5queueIP6ObjectSt5dequeIS1_SaIS1_EEE4pushERKS1_(ptr noundef nonnull align 8 dereferenceable(80) %24, ptr noundef nonnull align 8 dereferenceable(8) %4)
  %25 = getelementptr inbounds %class.VM, ptr %5, i32 0, i32 3
  %26 = load i32, ptr %25, align 8, !tbaa !14
  %27 = add nsw i32 %26, -1
  store i32 %27, ptr %25, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %35

28:                                               ; preds = %11
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Object, ptr %30, i32 0, i32 1
  store i32 0, ptr %31, align 4, !tbaa !9
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load ptr, ptr %32, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Object, ptr %33, i32 0, i32 2
  store ptr %34, ptr %3, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %28, %17
  br label %7, !llvm.loop !23

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt5queueIP6ObjectSt5dequeIS1_SaIS1_EEE4pushERKS1_(ptr noundef nonnull align 8 dereferenceable(80), ptr noundef nonnull align 8 dereferenceable(8)) #1 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 = { 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, !12, i64 4}
!10 = !{!"_ZTS6Object", !11, i64 0, !12, i64 4, !6, i64 8, !7, i64 16}
!11 = !{!"_ZTS10ObjectType", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 8}
!14 = !{!15, !12, i64 168}
!15 = !{!"_ZTS2VM", !16, i64 0, !6, i64 80, !22, i64 88, !12, i64 168, !12, i64 172, !12, i64 176}
!16 = !{!"_ZTSSt5dequeIP6ObjectSaIS1_EE", !17, i64 0}
!17 = !{!"_ZTSSt11_Deque_baseIP6ObjectSaIS1_EE", !18, i64 0}
!18 = !{!"_ZTSNSt11_Deque_baseIP6ObjectSaIS1_EE11_Deque_implE", !19, i64 0}
!19 = !{!"_ZTSNSt11_Deque_baseIP6ObjectSaIS1_EE16_Deque_impl_dataE", !6, i64 0, !20, i64 8, !21, i64 16, !21, i64 48}
!20 = !{!"long", !7, i64 0}
!21 = !{!"_ZTSSt15_Deque_iteratorIP6ObjectRS1_PS1_E", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!22 = !{!"_ZTSSt5queueIP6ObjectSt5dequeIS1_SaIS1_EEE", !16, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress 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 void @_ZN2VM5sweepEv(ptr noundef nonnull align 8 dereferenceable(180) %0) local_unnamed_addr #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = getelementptr inbounds i8, ptr %0, i64 80
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %.not9 = icmp eq ptr %4, null
  br i1 %.not9, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 88
  %6 = getelementptr inbounds i8, ptr %0, i64 168
  br label %7

7:                                                ; preds = %.lr.ph, %18
  %8 = phi ptr [ %4, %.lr.ph ], [ %19, %18 ]
  %.010 = phi ptr [ %3, %.lr.ph ], [ %.1, %18 ]
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %.not8 = icmp eq i32 %10, 0
  br i1 %.not8, label %11, label %16

11:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #2
  store ptr %8, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %8, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  store ptr %13, ptr %.010, align 8, !tbaa !5
  call void @_ZNSt5queueIP6ObjectSt5dequeIS1_SaIS1_EEE4pushERKS1_(ptr noundef nonnull align 8 dereferenceable(80) %5, ptr noundef nonnull align 8 dereferenceable(8) %2)
  %14 = load i32, ptr %6, align 8, !tbaa !14
  %15 = add nsw i32 %14, -1
  store i32 %15, ptr %6, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #2
  br label %18

16:                                               ; preds = %7
  store i32 0, ptr %9, align 4, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %8, i64 8
  br label %18

18:                                               ; preds = %16, %11
  %.1 = phi ptr [ %17, %16 ], [ %.010, %11 ]
  %19 = load ptr, ptr %.1, align 8, !tbaa !5
  %.not = icmp eq ptr %19, null
  br i1 %.not, label %._crit_edge, label %7, !llvm.loop !23

._crit_edge:                                      ; preds = %18, %1
  ret void
}

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

attributes #0 = { mustprogress 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 = { 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, !12, i64 4}
!10 = !{!"_ZTS6Object", !11, i64 0, !12, i64 4, !6, i64 8, !7, i64 16}
!11 = !{!"_ZTS10ObjectType", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 8}
!14 = !{!15, !12, i64 168}
!15 = !{!"_ZTS2VM", !16, i64 0, !6, i64 80, !22, i64 88, !12, i64 168, !12, i64 172, !12, i64 176}
!16 = !{!"_ZTSSt5dequeIP6ObjectSaIS1_EE", !17, i64 0}
!17 = !{!"_ZTSSt11_Deque_baseIP6ObjectSaIS1_EE", !18, i64 0}
!18 = !{!"_ZTSNSt11_Deque_baseIP6ObjectSaIS1_EE11_Deque_implE", !19, i64 0}
!19 = !{!"_ZTSNSt11_Deque_baseIP6ObjectSaIS1_EE16_Deque_impl_dataE", !6, i64 0, !20, i64 8, !21, i64 16, !21, i64 48}
!20 = !{!"long", !7, i64 0}
!21 = !{!"_ZTSSt15_Deque_iteratorIP6ObjectRS1_PS1_E", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!22 = !{!"_ZTSSt5queueIP6ObjectSt5dequeIS1_SaIS1_EEE", !16, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


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