; LLVM IR from DeanoC/gfx_imagecompress :: src/amd_hdr_encode.ll (ZN10HDR_Encode8mean_d_dEPA4_fPfii)

; 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 nounwind uwtable
define dso_local void @_ZN10HDR_Encode8mean_d_dEPA4_fPfii(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %20, %4
  %12 = load i32, ptr %10, align 4, !tbaa !9
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %15, label %23

15:                                               ; preds = %11
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load i32, ptr %10, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds float, ptr %16, i64 %18
  store float 0.000000e+00, ptr %19, align 4, !tbaa !11
  br label %20

20:                                               ; preds = %15
  %21 = load i32, ptr %10, align 4, !tbaa !9
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %10, align 4, !tbaa !9
  br label %11, !llvm.loop !13

23:                                               ; preds = %11
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %52, %23
  %25 = load i32, ptr %9, align 4, !tbaa !9
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %28, label %55

28:                                               ; preds = %24
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %48, %28
  %30 = load i32, ptr %10, align 4, !tbaa !9
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %33, label %51

33:                                               ; preds = %29
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [4 x float], ptr %34, i64 %36
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [4 x float], ptr %37, i64 0, i64 %39
  %41 = load float, ptr %40, align 4, !tbaa !11
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load i32, ptr %10, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds float, ptr %42, i64 %44
  %46 = load float, ptr %45, align 4, !tbaa !11
  %47 = fadd float %46, %41
  store float %47, ptr %45, align 4, !tbaa !11
  br label %48

48:                                               ; preds = %33
  %49 = load i32, ptr %10, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %10, align 4, !tbaa !9
  br label %29, !llvm.loop !15

51:                                               ; preds = %29
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %9, align 4, !tbaa !9
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %9, align 4, !tbaa !9
  br label %24, !llvm.loop !16

55:                                               ; preds = %24
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %56

56:                                               ; preds = %69, %55
  %57 = load i32, ptr %10, align 4, !tbaa !9
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = icmp slt i32 %57, %58
  br i1 %59, label %60, label %72

60:                                               ; preds = %56
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = sitofp i32 %61 to float
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load i32, ptr %10, align 4, !tbaa !9
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds float, ptr %63, i64 %65
  %67 = load float, ptr %66, align 4, !tbaa !11
  %68 = fdiv float %67, %62
  store float %68, ptr %66, align 4, !tbaa !11
  br label %69

69:                                               ; preds = %60
  %70 = load i32, ptr %10, align 4, !tbaa !9
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %10, align 4, !tbaa !9
  br label %56, !llvm.loop !17

72:                                               ; preds = %56
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !14}


; 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 memory(argmem: readwrite) uwtable
define dso_local void @_ZN10HDR_Encode8mean_d_dEPA4_fPfii(ptr nocapture noundef readonly %0, ptr nocapture noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp sgt i32 %3, 0
  br i1 %5, label %.preheader25, label %._crit_edge

.preheader25:                                     ; preds = %4
  %6 = zext nneg i32 %3 to i64
  %7 = shl nuw nsw i64 %6, 2
  tail call void @llvm.memset.p0.i64(ptr align 4 %1, i8 0, i64 %7, i1 false), !tbaa !5
  %8 = icmp sgt i32 %2, 0
  br i1 %8, label %.preheader24.us.preheader, label %.preheader

.preheader24.us.preheader:                        ; preds = %.preheader25
  %wide.trip.count38 = zext nneg i32 %2 to i64
  %wide.trip.count = zext nneg i32 %3 to i64
  %9 = shl nuw nsw i64 %wide.trip.count, 2
  %scevgep = getelementptr i8, ptr %1, i64 %9
  %10 = shl nuw nsw i64 %wide.trip.count38, 4
  %11 = getelementptr i8, ptr %0, i64 %10
  %12 = getelementptr i8, ptr %11, i64 %9
  %scevgep45 = getelementptr i8, ptr %12, i64 -16
  %min.iters.check = icmp ult i32 %3, 8
  %bound0 = icmp ugt ptr %scevgep45, %1
  %bound1 = icmp ugt ptr %scevgep, %0
  %found.conflict = and i1 %bound0, %bound1
  %n.vec = and i64 %wide.trip.count, 2147483640
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  %xtraiter = and i64 %wide.trip.count, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  %13 = add nsw i64 %wide.trip.count, -1
  br label %.preheader24.us

.preheader24.us:                                  ; preds = %.preheader24.us.preheader, %._crit_edge.us
  %indvars.iv35 = phi i64 [ 0, %.preheader24.us.preheader ], [ %indvars.iv.next36, %._crit_edge.us ]
  %brmerge = select i1 %min.iters.check, i1 true, i1 %found.conflict
  br i1 %brmerge, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.preheader24.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.preheader24.us ]
  %14 = getelementptr inbounds [4 x float], ptr %0, i64 %indvars.iv35, i64 %index
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  %wide.load = load <4 x float>, ptr %14, align 4, !tbaa !5, !alias.scope !9
  %wide.load46 = load <4 x float>, ptr %15, align 4, !tbaa !5, !alias.scope !9
  %16 = getelementptr inbounds float, ptr %1, i64 %index
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %wide.load47 = load <4 x float>, ptr %16, align 4, !tbaa !5, !alias.scope !12, !noalias !9
  %wide.load48 = load <4 x float>, ptr %17, align 4, !tbaa !5, !alias.scope !12, !noalias !9
  %18 = fadd <4 x float> %wide.load, %wide.load47
  %19 = fadd <4 x float> %wide.load46, %wide.load48
  store <4 x float> %18, ptr %16, align 4, !tbaa !5, !alias.scope !12, !noalias !9
  store <4 x float> %19, ptr %17, align 4, !tbaa !5, !alias.scope !12, !noalias !9
  %index.next = add nuw i64 %index, 8
  %20 = icmp eq i64 %index.next, %n.vec
  br i1 %20, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %.preheader24.us, %middle.block
  %indvars.iv.ph = phi i64 [ 0, %.preheader24.us ], [ %n.vec, %middle.block ]
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader
  %21 = getelementptr inbounds [4 x float], ptr %0, i64 %indvars.iv35, i64 %indvars.iv.ph
  %22 = load float, ptr %21, align 4, !tbaa !5
  %23 = getelementptr inbounds float, ptr %1, i64 %indvars.iv.ph
  %24 = load float, ptr %23, align 4, !tbaa !5
  %25 = fadd float %22, %24
  store float %25, ptr %23, align 4, !tbaa !5
  %indvars.iv.next.prol = or disjoint i64 %indvars.iv.ph, 1
  br label %scalar.ph.prol.loopexit

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %26 = icmp eq i64 %indvars.iv.ph, %13
  br i1 %26, label %._crit_edge.us, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %scalar.ph ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %27 = getelementptr inbounds [4 x float], ptr %0, i64 %indvars.iv35, i64 %indvars.iv
  %28 = load float, ptr %27, align 4, !tbaa !5
  %29 = getelementptr inbounds float, ptr %1, i64 %indvars.iv
  %30 = load float, ptr %29, align 4, !tbaa !5
  %31 = fadd float %28, %30
  store float %31, ptr %29, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %32 = getelementptr inbounds [4 x float], ptr %0, i64 %indvars.iv35, i64 %indvars.iv.next
  %33 = load float, ptr %32, align 4, !tbaa !5
  %34 = getelementptr inbounds float, ptr %1, i64 %indvars.iv.next
  %35 = load float, ptr %34, align 4, !tbaa !5
  %36 = fadd float %33, %35
  store float %36, ptr %34, align 4, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, %wide.trip.count
  br i1 %exitcond.not.1, label %._crit_edge.us, label %scalar.ph, !llvm.loop !18

._crit_edge.us:                                   ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %exitcond39.not = icmp eq i64 %indvars.iv.next36, %wide.trip.count38
  br i1 %exitcond39.not, label %.preheader, label %.preheader24.us, !llvm.loop !19

.preheader:                                       ; preds = %._crit_edge.us, %.preheader25
  br i1 %5, label %.lr.ph31, label %._crit_edge

.lr.ph31:                                         ; preds = %.preheader
  %37 = sitofp i32 %2 to float
  %wide.trip.count43 = zext nneg i32 %3 to i64
  %min.iters.check51 = icmp ult i32 %3, 4
  br i1 %min.iters.check51, label %scalar.ph50.preheader, label %vector.ph52

vector.ph52:                                      ; preds = %.lr.ph31
  %n.vec54 = and i64 %wide.trip.count43, 2147483644
  %broadcast.splatinsert = insertelement <4 x float> poison, float %37, i64 0
  %broadcast.splat = shufflevector <4 x float> %broadcast.splatinsert, <4 x float> poison, <4 x i32> zeroinitializer
  br label %vector.body56

vector.body56:                                    ; preds = %vector.body56, %vector.ph52
  %index57 = phi i64 [ 0, %vector.ph52 ], [ %index.next59, %vector.body56 ]
  %38 = getelementptr inbounds float, ptr %1, i64 %index57
  %wide.load58 = load <4 x float>, ptr %38, align 4, !tbaa !5
  %39 = fdiv <4 x float> %wide.load58, %broadcast.splat
  store <4 x float> %39, ptr %38, align 4, !tbaa !5
  %index.next59 = add nuw i64 %index57, 4
  %40 = icmp eq i64 %index.next59, %n.vec54
  br i1 %40, label %middle.block49, label %vector.body56, !llvm.loop !20

middle.block49:                                   ; preds = %vector.body56
  %cmp.n60 = icmp eq i64 %n.vec54, %wide.trip.count43
  br i1 %cmp.n60, label %._crit_edge, label %scalar.ph50.preheader

scalar.ph50.preheader:                            ; preds = %middle.block49, %.lr.ph31
  %indvars.iv40.ph = phi i64 [ 0, %.lr.ph31 ], [ %n.vec54, %middle.block49 ]
  br label %scalar.ph50

scalar.ph50:                                      ; preds = %scalar.ph50.preheader, %scalar.ph50
  %indvars.iv40 = phi i64 [ %indvars.iv.next41, %scalar.ph50 ], [ %indvars.iv40.ph, %scalar.ph50.preheader ]
  %41 = getelementptr inbounds float, ptr %1, i64 %indvars.iv40
  %42 = load float, ptr %41, align 4, !tbaa !5
  %43 = fdiv float %42, %37
  store float %43, ptr %41, align 4, !tbaa !5
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  %exitcond44.not = icmp eq i64 %indvars.iv.next41, %wide.trip.count43
  br i1 %exitcond44.not, label %._crit_edge, label %scalar.ph50, !llvm.loop !21

._crit_edge:                                      ; preds = %scalar.ph50, %middle.block49, %4, %.preheader
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) 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 nounwind willreturn memory(argmem: write) }

!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 = !{!10}
!10 = distinct !{!10, !11}
!11 = distinct !{!11, !"LVerDomain"}
!12 = !{!13}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}
!18 = distinct !{!18, !15, !16}
!19 = distinct !{!19, !15}
!20 = distinct !{!20, !15, !16, !17}
!21 = distinct !{!21, !15, !17, !16}


; Effective O3 passes: ADCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass