; LLVM IR from gem5/gem5 :: ext/softfloat (i64_to_f16)

; 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.float16_t = type { i16 }
%union.ui16_f16 = type { i16 }

; Function Attrs: nounwind uwtable
define dso_local i16 @i64_to_f16(i64 noundef %0) #0 {
  %2 = alloca %struct.float16_t, align 2
  %3 = alloca i64, align 8
  %4 = alloca i8, align 1
  %5 = alloca i64, align 8
  %6 = alloca i8, align 1
  %7 = alloca %union.ui16_f16, align 2
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = icmp slt i64 %10, 0
  %12 = zext i1 %11 to i8
  store i8 %12, ptr %4, align 1, !tbaa !9
  %13 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %14 = trunc i8 %13 to i1
  br i1 %14, label %15, label %18

15:                                               ; preds = %1
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = sub i64 0, %16
  br label %20

18:                                               ; preds = %1
  %19 = load i64, ptr %3, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %18, %15
  %21 = phi i64 [ %17, %15 ], [ %19, %18 ]
  store i64 %21, ptr %5, align 8, !tbaa !5
  %22 = load i64, ptr %5, align 8, !tbaa !5
  %23 = call zeroext i8 @softfloat_countLeadingZeros64(i64 noundef %22)
  %24 = zext i8 %23 to i32
  %25 = sub nsw i32 %24, 53
  %26 = trunc i32 %25 to i8
  store i8 %26, ptr %6, align 1, !tbaa !13
  %27 = load i8, ptr %6, align 1, !tbaa !13
  %28 = sext i8 %27 to i32
  %29 = icmp sle i32 0, %28
  br i1 %29, label %30, label %57

30:                                               ; preds = %20
  %31 = load i64, ptr %3, align 8, !tbaa !5
  %32 = icmp ne i64 %31, 0
  br i1 %32, label %33, label %53

33:                                               ; preds = %30
  %34 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %35 = trunc i8 %34 to i1
  %36 = zext i1 %35 to i16
  %37 = zext i16 %36 to i32
  %38 = shl i32 %37, 15
  %39 = load i8, ptr %6, align 1, !tbaa !13
  %40 = sext i8 %39 to i32
  %41 = sub nsw i32 24, %40
  %42 = trunc i32 %41 to i16
  %43 = zext i16 %42 to i32
  %44 = shl i32 %43, 10
  %45 = add nsw i32 %38, %44
  %46 = sext i32 %45 to i64
  %47 = load i64, ptr %5, align 8, !tbaa !5
  %48 = load i8, ptr %6, align 1, !tbaa !13
  %49 = sext i8 %48 to i32
  %50 = zext i32 %49 to i64
  %51 = shl i64 %47, %50
  %52 = add i64 %46, %51
  br label %54

53:                                               ; preds = %30
  br label %54

54:                                               ; preds = %53, %33
  %55 = phi i64 [ %52, %33 ], [ 0, %53 ]
  %56 = trunc i64 %55 to i16
  store i16 %56, ptr %7, align 2, !tbaa !13
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %2, ptr align 2 %7, i64 2, i1 false), !tbaa.struct !14
  store i32 1, ptr %9, align 4
  br label %89

57:                                               ; preds = %20
  %58 = load i8, ptr %6, align 1, !tbaa !13
  %59 = sext i8 %58 to i32
  %60 = add nsw i32 %59, 4
  %61 = trunc i32 %60 to i8
  store i8 %61, ptr %6, align 1, !tbaa !13
  %62 = load i8, ptr %6, align 1, !tbaa !13
  %63 = sext i8 %62 to i32
  %64 = icmp slt i32 %63, 0
  br i1 %64, label %65, label %72

65:                                               ; preds = %57
  %66 = load i64, ptr %5, align 8, !tbaa !5
  %67 = load i8, ptr %6, align 1, !tbaa !13
  %68 = sext i8 %67 to i32
  %69 = sub nsw i32 0, %68
  %70 = trunc i32 %69 to i8
  %71 = call i64 @softfloat_shortShiftRightJam64(i64 noundef %66, i8 noundef zeroext %70)
  br label %78

72:                                               ; preds = %57
  %73 = load i64, ptr %5, align 8, !tbaa !5
  %74 = load i8, ptr %6, align 1, !tbaa !13
  %75 = sext i8 %74 to i32
  %76 = zext i32 %75 to i64
  %77 = shl i64 %73, %76
  br label %78

78:                                               ; preds = %72, %65
  %79 = phi i64 [ %71, %65 ], [ %77, %72 ]
  store i64 %79, ptr %8, align 8, !tbaa !5
  %80 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %81 = trunc i8 %80 to i1
  %82 = load i8, ptr %6, align 1, !tbaa !13
  %83 = sext i8 %82 to i32
  %84 = sub nsw i32 28, %83
  %85 = sext i32 %84 to i64
  %86 = load i64, ptr %8, align 8, !tbaa !5
  %87 = call i16 @softfloat_roundPackToF16(i1 noundef zeroext %81, i64 noundef %85, i64 noundef %86)
  %88 = getelementptr inbounds %struct.float16_t, ptr %2, i32 0, i32 0
  store i16 %87, ptr %88, align 2
  store i32 1, ptr %9, align 4
  br label %89

89:                                               ; preds = %78, %54
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #5
  %90 = getelementptr inbounds %struct.float16_t, ptr %2, i32 0, i32 0
  %91 = load i16, ptr %90, align 2
  ret i16 %91
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare zeroext i8 @softfloat_countLeadingZeros64(i64 noundef) #2

; 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) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @softfloat_shortShiftRightJam64(i64 noundef, i8 noundef zeroext) #4

declare i16 @softfloat_roundPackToF16(i1 noundef zeroext, i64 noundef, i64 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { inlinehint 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 #5 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"_Bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!7, !7, i64 0}
!14 = !{i64 0, i64 2, !15}
!15 = !{!16, !16, i64 0}
!16 = !{!"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: nounwind uwtable
define dso_local i16 @i64_to_f16(i64 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i64 %0, 0
  %3 = tail call i64 @llvm.abs.i64(i64 %0, i1 false)
  %4 = tail call zeroext i8 @softfloat_countLeadingZeros64(i64 noundef %3) #4
  %5 = add i8 %4, -53
  %6 = icmp sgt i8 %5, -1
  br i1 %6, label %7, label %16

7:                                                ; preds = %1
  %.not = icmp eq i64 %0, 0
  br i1 %.not, label %31, label %8

8:                                                ; preds = %7
  %sh.diff = lshr i64 %0, 48
  %9 = and i64 %sh.diff, 32768
  %10 = zext nneg i8 %5 to i64
  %11 = shl i64 %3, %10
  %reass.sub = or disjoint i64 %9, 24576
  %12 = shl nuw nsw i64 %10, 10
  %13 = sub nsw i64 %reass.sub, %12
  %14 = add i64 %13, %11
  %15 = trunc i64 %14 to i16
  br label %31

16:                                               ; preds = %1
  %17 = add nsw i8 %4, -49
  %18 = sext i8 %17 to i32
  %19 = icmp slt i8 %4, 49
  br i1 %19, label %20, label %23

20:                                               ; preds = %16
  %21 = sub nsw i8 49, %4
  %22 = tail call i64 @softfloat_shortShiftRightJam64(i64 noundef %3, i8 noundef zeroext %21)
  br label %26

23:                                               ; preds = %16
  %24 = zext nneg i32 %18 to i64
  %25 = shl i64 %3, %24
  br label %26

26:                                               ; preds = %23, %20
  %27 = phi i64 [ %22, %20 ], [ %25, %23 ]
  %28 = sub nsw i32 28, %18
  %29 = zext nneg i32 %28 to i64
  %30 = tail call i16 @softfloat_roundPackToF16(i1 noundef zeroext %2, i64 noundef %29, i64 noundef %27) #4
  br label %31

31:                                               ; preds = %8, %7, %26
  %.sroa.020.0 = phi i16 [ %30, %26 ], [ %15, %8 ], [ 0, %7 ]
  ret i16 %.sroa.020.0
}

declare zeroext i8 @softfloat_countLeadingZeros64(i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @softfloat_shortShiftRightJam64(i64 noundef, i8 noundef zeroext) local_unnamed_addr #2

declare i16 @softfloat_roundPackToF16(i1 noundef zeroext, i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.abs.i64(i64, i1 immarg) #3

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 = { inlinehint 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)"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass