; LLVM IR from sbu-fsl/kernel-ml :: src/autodiff (autodiff)

; 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.linear_layer_functions_struct = type { ptr, ptr }
%struct.sigmoid_layer_functions_struct = type { ptr, ptr }
%struct.layers = type { ptr, ptr }
%struct.layer = type { ptr, i32, ptr, ptr }

@linear_layer_functions = external global %struct.linear_layer_functions_struct, align 8
@sigmoid_layer_functions = external global %struct.sigmoid_layer_functions_struct, align 8

; 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 @autodiff_backward(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr null, ptr %7, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %10, ptr %6, align 8, !tbaa !5
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.layers, ptr %11, i32 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  store ptr %13, ptr %5, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %52, %2
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %56

17:                                               ; preds = %14
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.layer, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 8, !tbaa !11
  switch i32 %20, label %37 [
    i32 0, label %21
    i32 1, label %29
  ]

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.layer, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  store ptr %24, ptr %8, align 8, !tbaa !5
  %25 = load ptr, ptr getelementptr inbounds (%struct.linear_layer_functions_struct, ptr @linear_layer_functions, i32 0, i32 1), align 8, !tbaa !15
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = call ptr %25(ptr noundef %26, ptr noundef %27)
  store ptr %28, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %37

29:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.layer, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !14
  store ptr %32, ptr %9, align 8, !tbaa !5
  %33 = load ptr, ptr getelementptr inbounds (%struct.sigmoid_layer_functions_struct, ptr @sigmoid_layer_functions, i32 0, i32 1), align 8, !tbaa !17
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = call ptr %33(ptr noundef %34, ptr noundef %35)
  store ptr %36, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %37

37:                                               ; preds = %29, %21, %17
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %50

40:                                               ; preds = %37
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = icmp ne ptr %41, %42
  br i1 %43, label %44, label %50

44:                                               ; preds = %40
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = icmp ne ptr %45, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %44
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free_matrix(ptr noundef %49)
  br label %50

50:                                               ; preds = %48, %44, %40, %37
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %51, ptr %6, align 8, !tbaa !5
  br label %52

52:                                               ; preds = %50
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.layer, ptr %53, i32 0, i32 3
  %55 = load ptr, ptr %54, align 8, !tbaa !19
  store ptr %55, ptr %5, align 8, !tbaa !5
  br label %14, !llvm.loop !20

56:                                               ; preds = %14
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free_matrix(ptr noundef %57)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void
}

declare void @free_matrix(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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"layers", !6, i64 0, !6, i64 8}
!11 = !{!12, !13, i64 8}
!12 = !{!"layer", !6, i64 0, !13, i64 8, !6, i64 16, !6, i64 24}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !6, i64 0}
!15 = !{!16, !6, i64 8}
!16 = !{!"linear_layer_functions_struct", !6, i64 0, !6, i64 8}
!17 = !{!18, !6, i64 8}
!18 = !{!"sigmoid_layer_functions_struct", !6, i64 0, !6, i64 8}
!19 = !{!12, !6, i64 24}
!20 = distinct !{!20, !21}
!21 = !{!"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"

%struct.linear_layer_functions_struct = type { ptr, ptr }
%struct.sigmoid_layer_functions_struct = type { ptr, ptr }

@linear_layer_functions = external local_unnamed_addr global %struct.linear_layer_functions_struct, align 8
@sigmoid_layer_functions = external local_unnamed_addr global %struct.sigmoid_layer_functions_struct, align 8

; Function Attrs: nounwind uwtable
define dso_local void @autodiff_backward(ptr nocapture noundef readonly %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %.025 = load ptr, ptr %3, align 8, !tbaa !5
  %.not26 = icmp eq ptr %.025, null
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %12
  %.029 = phi ptr [ %.0, %12 ], [ %.025, %2 ]
  %.01928 = phi ptr [ %.1, %12 ], [ %1, %2 ]
  %.02027 = phi ptr [ %.1, %12 ], [ null, %2 ]
  %4 = getelementptr inbounds i8, ptr %.029, i64 8
  %5 = load i32, ptr %4, align 8, !tbaa !9
  switch i32 %5, label %10 [
    i32 0, label %.sink.split
    i32 1, label %6
  ]

6:                                                ; preds = %.lr.ph
  br label %.sink.split

.sink.split:                                      ; preds = %.lr.ph, %6
  %.sink31 = phi ptr [ getelementptr inbounds (i8, ptr @sigmoid_layer_functions, i64 8), %6 ], [ getelementptr inbounds (i8, ptr @linear_layer_functions, i64 8), %.lr.ph ]
  %7 = load ptr, ptr %.029, align 8, !tbaa !12
  %8 = load ptr, ptr %.sink31, align 8, !tbaa !5
  %9 = tail call ptr %8(ptr noundef %.01928, ptr noundef %7) #2
  br label %10

10:                                               ; preds = %.sink.split, %.lr.ph
  %.1 = phi ptr [ %.02027, %.lr.ph ], [ %9, %.sink.split ]
  %.not21 = icmp eq ptr %.01928, null
  %.not22 = icmp eq ptr %.01928, %.1
  %or.cond = select i1 %.not21, i1 true, i1 %.not22
  %.not23 = icmp eq ptr %.01928, %1
  %or.cond24 = or i1 %.not23, %or.cond
  br i1 %or.cond24, label %12, label %11

11:                                               ; preds = %10
  tail call void @free_matrix(ptr noundef nonnull %.01928) #2
  br label %12

12:                                               ; preds = %11, %10
  %13 = getelementptr inbounds i8, ptr %.029, i64 24
  %.0 = load ptr, ptr %13, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %12, %2
  %.019.lcssa = phi ptr [ %1, %2 ], [ %.1, %12 ]
  tail call void @free_matrix(ptr noundef %.019.lcssa) #2
  ret void
}

declare void @free_matrix(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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"layer", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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