; LLVM IR from heroseh/cgm :: cgm (CgmF16_from_float)

; 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.CgmF16 = type { i16 }
%union._cgm_float_uint = type { float }

; 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 i16 @CgmF16_from_float(float noundef %0) #1 {
  %2 = alloca %struct.CgmF16, align 2
  %3 = alloca float, align 4
  %4 = alloca %union._cgm_float_uint, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store float %0, ptr %3, align 4, !tbaa !5
  %8 = load float, ptr %3, align 4, !tbaa !5
  %9 = call float @llvm.fabs.f32(float %8) #3
  %10 = fcmp oeq float %9, 0x7FF0000000000000
  %11 = bitcast float %8 to i32
  %12 = icmp slt i32 %11, 0
  %13 = select i1 %12, i32 -1, i32 1
  %14 = select i1 %10, i32 %13, i32 0
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %24

16:                                               ; preds = %1
  %17 = getelementptr inbounds %struct.CgmF16, ptr %2, i32 0, i32 0
  %18 = load float, ptr %3, align 4, !tbaa !5
  %19 = fpext float %18 to double
  %20 = fcmp olt double %19, 0.000000e+00
  %21 = zext i1 %20 to i64
  %22 = select i1 %20, i32 64512, i32 31744
  %23 = trunc i32 %22 to i16
  store i16 %23, ptr %17, align 2, !tbaa !9
  br label %70

24:                                               ; preds = %1
  %25 = load float, ptr %3, align 4, !tbaa !5
  %26 = call i1 @llvm.is.fpclass.f32(float %25, i32 3)
  br i1 %26, label %27, label %29

27:                                               ; preds = %24
  %28 = getelementptr inbounds %struct.CgmF16, ptr %2, i32 0, i32 0
  store i16 -1, ptr %28, align 2, !tbaa !9
  br label %70

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %30 = load float, ptr %3, align 4, !tbaa !5
  store float %30, ptr %4, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %31 = load i32, ptr %4, align 4, !tbaa !12
  %32 = and i32 %31, 2147483647
  store i32 %32, ptr %5, align 4, !tbaa !13
  %33 = load i32, ptr %4, align 4, !tbaa !12
  %34 = and i32 %33, -2147483648
  store i32 %34, ptr %6, align 4, !tbaa !13
  %35 = load i32, ptr %4, align 4, !tbaa !12
  %36 = and i32 %35, 2139095040
  store i32 %36, ptr %7, align 4, !tbaa !13
  %37 = load i32, ptr %5, align 4, !tbaa !13
  %38 = lshr i32 %37, 13
  store i32 %38, ptr %5, align 4, !tbaa !13
  %39 = load i32, ptr %6, align 4, !tbaa !13
  %40 = lshr i32 %39, 16
  store i32 %40, ptr %6, align 4, !tbaa !13
  %41 = load i32, ptr %5, align 4, !tbaa !13
  %42 = sub i32 %41, 114688
  store i32 %42, ptr %5, align 4, !tbaa !13
  %43 = load i32, ptr %7, align 4, !tbaa !13
  %44 = icmp ult i32 %43, 947912704
  br i1 %44, label %45, label %46

45:                                               ; preds = %29
  br label %48

46:                                               ; preds = %29
  %47 = load i32, ptr %5, align 4, !tbaa !13
  br label %48

48:                                               ; preds = %46, %45
  %49 = phi i32 [ 0, %45 ], [ %47, %46 ]
  store i32 %49, ptr %5, align 4, !tbaa !13
  %50 = load i32, ptr %7, align 4, !tbaa !13
  %51 = icmp ugt i32 %50, -1912602624
  br i1 %51, label %52, label %53

52:                                               ; preds = %48
  br label %55

53:                                               ; preds = %48
  %54 = load i32, ptr %5, align 4, !tbaa !13
  br label %55

55:                                               ; preds = %53, %52
  %56 = phi i32 [ 31743, %52 ], [ %54, %53 ]
  store i32 %56, ptr %5, align 4, !tbaa !13
  %57 = load i32, ptr %7, align 4, !tbaa !13
  %58 = icmp eq i32 %57, 0
  br i1 %58, label %59, label %60

59:                                               ; preds = %55
  br label %62

60:                                               ; preds = %55
  %61 = load i32, ptr %5, align 4, !tbaa !13
  br label %62

62:                                               ; preds = %60, %59
  %63 = phi i32 [ 0, %59 ], [ %61, %60 ]
  store i32 %63, ptr %5, align 4, !tbaa !13
  %64 = load i32, ptr %6, align 4, !tbaa !13
  %65 = load i32, ptr %5, align 4, !tbaa !13
  %66 = or i32 %65, %64
  store i32 %66, ptr %5, align 4, !tbaa !13
  %67 = getelementptr inbounds %struct.CgmF16, ptr %2, i32 0, i32 0
  %68 = load i32, ptr %5, align 4, !tbaa !13
  %69 = trunc i32 %68 to i16
  store i16 %69, ptr %67, align 2, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %70

70:                                               ; preds = %62, %27, %16
  %71 = getelementptr inbounds %struct.CgmF16, ptr %2, i32 0, i32 0
  %72 = load i16, ptr %71, align 2
  ret i16 %72
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i1 @llvm.is.fpclass.f32(float, i32 immarg) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { memory(none) }
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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"CgmF16", !11, i64 0}
!11 = !{!"short", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}


; 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 nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local i16 @CgmF16_from_float(float noundef %0) local_unnamed_addr #0 {
  %2 = tail call float @llvm.fabs.f32(float %0) #2
  %3 = fcmp oeq float %2, 0x7FF0000000000000
  %4 = bitcast float %0 to i32
  br i1 %3, label %5, label %8

5:                                                ; preds = %1
  %6 = fcmp olt float %0, 0.000000e+00
  %7 = select i1 %6, i16 -1024, i16 31744
  br label %21

8:                                                ; preds = %1
  %9 = fcmp uno float %0, 0.000000e+00
  br i1 %9, label %21, label %10

10:                                               ; preds = %8
  %11 = bitcast float %2 to i32
  %12 = and i32 %4, 2139095040
  %13 = lshr i32 %11, 13
  %14 = lshr i32 %4, 16
  %15 = and i32 %14, 32768
  %16 = add nuw nsw i32 %13, 16384
  %17 = icmp ult i32 %12, 947912704
  %18 = select i1 %17, i32 947912704, i32 %16
  %19 = or i32 %18, %15
  %20 = trunc i32 %19 to i16
  br label %21

21:                                               ; preds = %8, %10, %5
  %.sroa.017.0 = phi i16 [ %7, %5 ], [ %20, %10 ], [ -1, %8 ]
  ret i16 %.sroa.017.0
}

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) 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 = { 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)"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass