; LLVM IR from onnx/onnx-mlir :: src/Support (SmallFPConversion)

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; 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 zeroext i16 @om_f32_to_bf16(float noundef %0) #2 {
  %2 = alloca i16, align 2
  %3 = alloca float, align 4
  %4 = alloca i32, align 4
  %5 = alloca i16, align 2
  %6 = alloca i32, align 4
  store float %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %4, ptr align 4 %3, i64 4, i1 false)
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = and i32 %7, 131071
  %9 = icmp eq i32 %8, 98304
  %10 = zext i1 %9 to i32
  %11 = mul nsw i32 %10, 65536
  %12 = add nsw i32 32767, %11
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = add i32 %13, %12
  store i32 %14, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #4
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = lshr i32 %15, 16
  %17 = trunc i32 %16 to i16
  store i16 %17, ptr %5, align 2, !tbaa !11
  %18 = load i16, ptr %5, align 2, !tbaa !11
  %19 = zext i16 %18 to i32
  %20 = and i32 %19, 32767
  %21 = icmp eq i32 %20, 32640
  br i1 %21, label %22, label %30

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

25:                                               ; preds = %22
  %26 = load i16, ptr %5, align 2, !tbaa !11
  %27 = zext i16 %26 to i32
  %28 = add nsw i32 %27, 64
  %29 = trunc i32 %28 to i16
  store i16 %29, ptr %2, align 2
  store i32 1, ptr %6, align 4
  br label %45

30:                                               ; preds = %22, %1
  %31 = load i16, ptr %5, align 2, !tbaa !11
  %32 = zext i16 %31 to i32
  %33 = and i32 %32, 32767
  %34 = icmp eq i32 %33, 0
  br i1 %34, label %35, label %43

35:                                               ; preds = %30
  %36 = load float, ptr %3, align 4, !tbaa !5
  %37 = call i1 @llvm.is.fpclass.f32(float %36, i32 3)
  br i1 %37, label %38, label %43

38:                                               ; preds = %35
  %39 = load i16, ptr %5, align 2, !tbaa !11
  %40 = zext i16 %39 to i32
  %41 = sub nsw i32 %40, 1
  %42 = trunc i32 %41 to i16
  store i16 %42, ptr %2, align 2
  store i32 1, ptr %6, align 4
  br label %45

43:                                               ; preds = %35, %30
  %44 = load i16, ptr %5, align 2, !tbaa !11
  store i16 %44, ptr %2, align 2
  store i32 1, ptr %6, align 4
  br label %45

45:                                               ; preds = %43, %38, %25
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %46 = load i16, ptr %2, align 2
  ret i16 %46
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i1 @llvm.is.fpclass.f32(float, i32 immarg) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !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 zeroext i16 @om_f32_to_bf16(float noundef %0) local_unnamed_addr #0 {
  %2 = bitcast float %0 to i32
  %3 = and i32 %2, 131071
  %4 = icmp eq i32 %3, 98304
  %5 = select i1 %4, i32 98303, i32 32767
  %6 = add i32 %5, %2
  %7 = lshr i32 %6, 16
  %8 = trunc nuw i32 %7 to i16
  %9 = and i32 %7, 32767
  %10 = icmp eq i32 %9, 32640
  %11 = fcmp uno float %0, 0.000000e+00
  %or.cond = and i1 %11, %10
  br i1 %or.cond, label %12, label %14

12:                                               ; preds = %1
  %13 = or disjoint i16 %8, 64
  br label %17

14:                                               ; preds = %1
  %15 = icmp eq i32 %9, 0
  %or.cond11 = and i1 %11, %15
  %16 = sext i1 %or.cond11 to i16
  %spec.select = add i16 %16, %8
  br label %17

17:                                               ; preds = %14, %12
  %.0 = phi i16 [ %13, %12 ], [ %spec.select, %14 ]
  ret i16 %.0
}

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

!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,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass