; LLVM IR from ermig1979/Simd :: src/Simd/SimdBaseFloat32.ll (ZN4Simd4Base17CosineDistance32fEPKfS2_mPf)

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

; 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 @_ZN4Simd4Base17CosineDistance32fEPKfS2_mPf(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca float, align 4
  %10 = alloca float, align 4
  %11 = alloca float, align 4
  %12 = alloca i64, align 8
  %13 = alloca float, align 4
  %14 = alloca float, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store float 0.000000e+00, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store float 0.000000e+00, ptr %10, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store float 0.000000e+00, ptr %11, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %41, %4
  %16 = load i64, ptr %12, align 8, !tbaa !9
  %17 = load i64, ptr %7, align 8, !tbaa !9
  %18 = icmp ult i64 %16, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %44

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = load i64, ptr %12, align 8, !tbaa !9
  %23 = getelementptr inbounds float, ptr %21, i64 %22
  %24 = load float, ptr %23, align 4, !tbaa !11
  store float %24, ptr %13, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load i64, ptr %12, align 8, !tbaa !9
  %27 = getelementptr inbounds float, ptr %25, i64 %26
  %28 = load float, ptr %27, align 4, !tbaa !11
  store float %28, ptr %14, align 4, !tbaa !11
  %29 = load float, ptr %13, align 4, !tbaa !11
  %30 = load float, ptr %13, align 4, !tbaa !11
  %31 = load float, ptr %9, align 4, !tbaa !11
  %32 = call float @llvm.fmuladd.f32(float %29, float %30, float %31)
  store float %32, ptr %9, align 4, !tbaa !11
  %33 = load float, ptr %13, align 4, !tbaa !11
  %34 = load float, ptr %14, align 4, !tbaa !11
  %35 = load float, ptr %10, align 4, !tbaa !11
  %36 = call float @llvm.fmuladd.f32(float %33, float %34, float %35)
  store float %36, ptr %10, align 4, !tbaa !11
  %37 = load float, ptr %14, align 4, !tbaa !11
  %38 = load float, ptr %14, align 4, !tbaa !11
  %39 = load float, ptr %11, align 4, !tbaa !11
  %40 = call float @llvm.fmuladd.f32(float %37, float %38, float %39)
  store float %40, ptr %11, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %41

41:                                               ; preds = %20
  %42 = load i64, ptr %12, align 8, !tbaa !9
  %43 = add i64 %42, 1
  store i64 %43, ptr %12, align 8, !tbaa !9
  br label %15, !llvm.loop !13

44:                                               ; preds = %19
  %45 = load float, ptr %10, align 4, !tbaa !11
  %46 = load float, ptr %9, align 4, !tbaa !11
  %47 = load float, ptr %11, align 4, !tbaa !11
  %48 = fmul float %46, %47
  %49 = call noundef float @_ZSt4sqrtf(float noundef %48)
  %50 = fdiv float %45, %49
  %51 = fsub float 1.000000e+00, %50
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  store float %51, ptr %52, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef float @_ZSt4sqrtf(float noundef) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #4 = { 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, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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 nounwind uwtable
define dso_local void @_ZN4Simd4Base17CosineDistance32fEPKfS2_mPf(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %xtraiter = and i64 %2, 1
  %5 = icmp eq i64 %2, 1
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %2, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa34.ph = phi float [ poison, %.lr.ph.preheader ], [ %39, %.lr.ph ]
  %.lcssa.ph = phi <2 x float> [ poison, %.lr.ph.preheader ], [ %44, %.lr.ph ]
  %.025.unr = phi float [ 0.000000e+00, %.lr.ph.preheader ], [ %39, %.lr.ph ]
  %.02023.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %45, %.lr.ph ]
  %.unr = phi <2 x float> [ zeroinitializer, %.lr.ph.preheader ], [ %44, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %6 = getelementptr inbounds float, ptr %0, i64 %.02023.unr
  %7 = load float, ptr %6, align 4, !tbaa !5
  %8 = getelementptr inbounds float, ptr %1, i64 %.02023.unr
  %9 = load float, ptr %8, align 4, !tbaa !5
  %10 = tail call float @llvm.fmuladd.f32(float %7, float %7, float %.025.unr)
  %11 = insertelement <2 x float> poison, float %9, i64 0
  %12 = shufflevector <2 x float> %11, <2 x float> poison, <2 x i32> zeroinitializer
  %13 = insertelement <2 x float> poison, float %7, i64 0
  %14 = insertelement <2 x float> %13, float %9, i64 1
  %15 = tail call <2 x float> @llvm.fmuladd.v2f32(<2 x float> %12, <2 x float> %14, <2 x float> %.unr)
  br label %._crit_edge.loopexit

._crit_edge.loopexit:                             ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.lcssa34 = phi float [ %.lcssa34.ph, %._crit_edge.loopexit.unr-lcssa ], [ %10, %.lr.ph.epil ]
  %.lcssa = phi <2 x float> [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %15, %.lr.ph.epil ]
  %16 = extractelement <2 x float> %.lcssa, i64 1
  %17 = fmul float %16, %.lcssa34
  %18 = extractelement <2 x float> %.lcssa, i64 0
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %4
  %.019.lcssa = phi float [ 0.000000e+00, %4 ], [ %18, %._crit_edge.loopexit ]
  %19 = phi float [ 0.000000e+00, %4 ], [ %17, %._crit_edge.loopexit ]
  %20 = tail call noundef float @_ZSt4sqrtf(float noundef %19)
  %21 = fdiv float %.019.lcssa, %20
  %22 = fsub float 1.000000e+00, %21
  store float %22, ptr %3, align 4, !tbaa !5
  ret void

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.025 = phi float [ 0.000000e+00, %.lr.ph.preheader.new ], [ %39, %.lr.ph ]
  %.02023 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %45, %.lr.ph ]
  %23 = phi <2 x float> [ zeroinitializer, %.lr.ph.preheader.new ], [ %44, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %24 = getelementptr inbounds float, ptr %0, i64 %.02023
  %25 = load float, ptr %24, align 4, !tbaa !5
  %26 = getelementptr inbounds float, ptr %1, i64 %.02023
  %27 = load float, ptr %26, align 4, !tbaa !5
  %28 = tail call float @llvm.fmuladd.f32(float %25, float %25, float %.025)
  %29 = insertelement <2 x float> poison, float %27, i64 0
  %30 = shufflevector <2 x float> %29, <2 x float> poison, <2 x i32> zeroinitializer
  %31 = insertelement <2 x float> poison, float %25, i64 0
  %32 = insertelement <2 x float> %31, float %27, i64 1
  %33 = tail call <2 x float> @llvm.fmuladd.v2f32(<2 x float> %30, <2 x float> %32, <2 x float> %23)
  %34 = or disjoint i64 %.02023, 1
  %35 = getelementptr inbounds float, ptr %0, i64 %34
  %36 = load float, ptr %35, align 4, !tbaa !5
  %37 = getelementptr inbounds float, ptr %1, i64 %34
  %38 = load float, ptr %37, align 4, !tbaa !5
  %39 = tail call float @llvm.fmuladd.f32(float %36, float %36, float %28)
  %40 = insertelement <2 x float> poison, float %38, i64 0
  %41 = shufflevector <2 x float> %40, <2 x float> poison, <2 x i32> zeroinitializer
  %42 = insertelement <2 x float> poison, float %36, i64 0
  %43 = insertelement <2 x float> %42, float %38, i64 1
  %44 = tail call <2 x float> @llvm.fmuladd.v2f32(<2 x float> %41, <2 x float> %43, <2 x float> %33)
  %45 = add nuw i64 %.02023, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef float @_ZSt4sqrtf(float noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <2 x float> @llvm.fmuladd.v2f32(<2 x float>, <2 x float>, <2 x float>) #3

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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