; LLVM IR from ARM-software/Arm-2D :: examples/common/controls (histogram)

; 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.histogram_t = type { %struct.anon.15 }
%struct.anon.15 = type { %struct.histogram_cfg_t, %struct.arm_2d_size_t, %struct.anon.16, i8, i8, i32 }
%struct.histogram_cfg_t = type { %struct.anon.13, %struct.anon.14, ptr, %struct.histogram_on_get_bin_value_evt_t }
%struct.anon.13 = type { %struct.arm_2d_size_t, ptr, i16, i8, i8, i32, ptr }
%struct.anon.14 = type { i32, i32 }
%struct.histogram_on_get_bin_value_evt_t = type { ptr, ptr }
%struct.arm_2d_size_t = type { i16, i16 }
%struct.anon.16 = type { %struct.arm_2d_region_list_item_t, i16 }
%struct.arm_2d_region_list_item_t = type { ptr, [8 x i8], %struct.arm_2d_region_t, [4 x i8] }
%struct.arm_2d_region_t = type { %union.anon.9, %union.anon.10 }
%union.anon.9 = type { %struct.arm_2d_location_t }
%struct.arm_2d_location_t = type { i16, i16 }
%union.anon.10 = type { %struct.arm_2d_size_t }
%struct.histogram_bin_item_t = type { %struct.anon.17 }
%struct.anon.17 = type { i16, i16, i16 }

@.str = external hidden unnamed_addr constant [15 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [90 x i8], align 1
@__PRETTY_FUNCTION__.histogram_on_frame_start = external hidden unnamed_addr constant [45 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @histogram_on_frame_start(ptr noundef nonnull %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = icmp ne ptr null, %6
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  br label %10

9:                                                ; preds = %1
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 220, ptr noundef @__PRETTY_FUNCTION__.histogram_on_frame_start) #4
  unreachable

10:                                               ; preds = %8
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.histogram_t, ptr %11, i32 0, i32 0
  %13 = getelementptr inbounds %struct.anon.15, ptr %12, i32 0, i32 0
  %14 = getelementptr inbounds %struct.histogram_cfg_t, ptr %13, i32 0, i32 3
  %15 = getelementptr inbounds %struct.histogram_on_get_bin_value_evt_t, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !9
  %17 = icmp ne ptr null, %16
  br i1 %17, label %18, label %90

18:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.histogram_t, ptr %19, i32 0, i32 0
  %21 = getelementptr inbounds %struct.anon.15, ptr %20, i32 0, i32 0
  %22 = getelementptr inbounds %struct.histogram_cfg_t, ptr %21, i32 0, i32 0
  %23 = getelementptr inbounds %struct.anon.13, ptr %22, i32 0, i32 1
  %24 = load ptr, ptr %23, align 8, !tbaa !22
  store ptr %24, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  store i64 0, ptr %4, align 8, !tbaa !23
  br label %25

25:                                               ; preds = %86, %18
  %26 = load i64, ptr %4, align 8, !tbaa !23
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.histogram_t, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds %struct.anon.15, ptr %28, i32 0, i32 0
  %30 = getelementptr inbounds %struct.histogram_cfg_t, ptr %29, i32 0, i32 0
  %31 = getelementptr inbounds %struct.anon.13, ptr %30, i32 0, i32 2
  %32 = load i16, ptr %31, align 8, !tbaa !25
  %33 = zext i16 %32 to i64
  %34 = icmp ult i64 %26, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  br label %89

36:                                               ; preds = %25
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.histogram_bin_item_t, ptr %37, i32 0, i32 0
  %39 = getelementptr inbounds %struct.anon.17, ptr %38, i32 0, i32 0
  %40 = load i16, ptr %39, align 2, !tbaa !26
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.histogram_bin_item_t, ptr %41, i32 0, i32 0
  %43 = getelementptr inbounds %struct.anon.17, ptr %42, i32 0, i32 1
  store i16 %40, ptr %43, align 2, !tbaa !29
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.histogram_t, ptr %44, i32 0, i32 0
  %46 = getelementptr inbounds %struct.anon.15, ptr %45, i32 0, i32 0
  %47 = getelementptr inbounds %struct.histogram_cfg_t, ptr %46, i32 0, i32 3
  %48 = getelementptr inbounds %struct.histogram_on_get_bin_value_evt_t, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !9
  %50 = icmp eq ptr null, %49
  br i1 %50, label %51, label %52

51:                                               ; preds = %36
  br label %68

52:                                               ; preds = %36
  %53 = load ptr, ptr %2, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.histogram_t, ptr %53, i32 0, i32 0
  %55 = getelementptr inbounds %struct.anon.15, ptr %54, i32 0, i32 0
  %56 = getelementptr inbounds %struct.histogram_cfg_t, ptr %55, i32 0, i32 3
  %57 = getelementptr inbounds %struct.histogram_on_get_bin_value_evt_t, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !9
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.histogram_t, ptr %59, i32 0, i32 0
  %61 = getelementptr inbounds %struct.anon.15, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds %struct.histogram_cfg_t, ptr %61, i32 0, i32 3
  %63 = getelementptr inbounds %struct.histogram_on_get_bin_value_evt_t, ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %63, align 8, !tbaa !30
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = load i64, ptr %4, align 8, !tbaa !23
  %67 = call i32 %58(ptr noundef %64, ptr noundef %65, i64 noundef %66)
  br label %68

68:                                               ; preds = %52, %51
  %69 = phi i32 [ 0, %51 ], [ %67, %52 ]
  store i32 %69, ptr %5, align 4, !tbaa !31
  %70 = load ptr, ptr %2, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.histogram_t, ptr %70, i32 0, i32 0
  %72 = getelementptr inbounds %struct.anon.15, ptr %71, i32 0, i32 5
  %73 = load i32, ptr %72, align 4, !tbaa !32
  %74 = sext i32 %73 to i64
  %75 = load i32, ptr %5, align 4, !tbaa !31
  %76 = sext i32 %75 to i64
  %77 = shl i64 %76, 16
  %78 = mul nsw i64 %74, %77
  %79 = ashr i64 %78, 32
  %80 = trunc i64 %79 to i16
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.histogram_bin_item_t, ptr %81, i32 0, i32 0
  %83 = getelementptr inbounds %struct.anon.17, ptr %82, i32 0, i32 0
  store i16 %80, ptr %83, align 2, !tbaa !26
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.histogram_bin_item_t, ptr %84, i32 1
  store ptr %85, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %86

86:                                               ; preds = %68
  %87 = load i64, ptr %4, align 8, !tbaa !23
  %88 = add i64 %87, 1
  store i64 %88, ptr %4, align 8, !tbaa !23
  br label %25, !llvm.loop !33

89:                                               ; preds = %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  br label %90

90:                                               ; preds = %89, %10
  %91 = load ptr, ptr %2, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.histogram_t, ptr %91, i32 0, i32 0
  %93 = getelementptr inbounds %struct.anon.15, ptr %92, i32 0, i32 3
  %94 = load i8, ptr %93, align 8
  %95 = and i8 %94, 1
  %96 = icmp ne i8 %95, 0
  br i1 %96, label %97, label %102

97:                                               ; preds = %90
  %98 = load ptr, ptr %2, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.histogram_t, ptr %98, i32 0, i32 0
  %100 = getelementptr inbounds %struct.anon.15, ptr %99, i32 0, i32 2
  %101 = getelementptr inbounds %struct.anon.16, ptr %100, i32 0, i32 0
  call void @arm_2d_dynamic_dirty_region_on_frame_start(ptr noundef %101, i8 noundef zeroext 0)
  br label %102

102:                                              ; preds = %97, %90
  ret void
}

declare void @arm_2d_dynamic_dirty_region_on_frame_start(ptr noundef, i8 noundef zeroext) #3

attributes #0 = { noreturn nounwind "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 = { 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 = { "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 = { noreturn nounwind }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 48}
!10 = !{!"histogram_t", !11, i64 0}
!11 = !{!"", !12, i64 0, !14, i64 64, !19, i64 72, !7, i64 112, !7, i64 112, !7, i64 113, !16, i64 116}
!12 = !{!"histogram_cfg_t", !13, i64 0, !17, i64 32, !6, i64 40, !18, i64 48}
!13 = !{!"", !14, i64 0, !6, i64 8, !15, i64 16, !7, i64 18, !7, i64 19, !7, i64 19, !7, i64 19, !16, i64 20, !6, i64 24}
!14 = !{!"arm_2d_size_t", !15, i64 0, !15, i64 2}
!15 = !{!"short", !7, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!"", !16, i64 0, !16, i64 4}
!18 = !{!"histogram_on_get_bin_value_evt_t", !6, i64 0, !6, i64 8}
!19 = !{!"", !20, i64 0, !15, i64 32}
!20 = !{!"arm_2d_region_list_item_t", !6, i64 0, !7, i64 8, !21, i64 16, !7, i64 24}
!21 = !{!"arm_2d_region_t", !7, i64 0, !7, i64 4}
!22 = !{!10, !6, i64 8}
!23 = !{!24, !24, i64 0}
!24 = !{!"long", !7, i64 0}
!25 = !{!10, !15, i64 16}
!26 = !{!27, !15, i64 0}
!27 = !{!"histogram_bin_item_t", !28, i64 0}
!28 = !{!"", !15, i64 0, !15, i64 2, !15, i64 4}
!29 = !{!27, !15, i64 2}
!30 = !{!10, !6, i64 56}
!31 = !{!16, !16, i64 0}
!32 = !{!10, !16, i64 116}
!33 = distinct !{!33, !34}
!34 = !{!"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: nounwind uwtable
define dso_local void @histogram_on_frame_start(ptr noundef nonnull %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 48
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.loopexit, label %4

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load i16, ptr %5, align 8, !tbaa !21
  %.not23 = icmp eq i16 %6, 0
  br i1 %.not23, label %.loopexit, label %.lr.ph.split.preheader

.lr.ph.split.preheader:                           ; preds = %4
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !22
  %9 = getelementptr inbounds i8, ptr %0, i64 56
  %10 = getelementptr inbounds i8, ptr %0, i64 116
  br label %.lr.ph.split

.lr.ph.split:                                     ; preds = %.lr.ph.split.preheader, %19
  %11 = phi i16 [ %20, %19 ], [ %6, %.lr.ph.split.preheader ]
  %.022 = phi ptr [ %29, %19 ], [ %8, %.lr.ph.split.preheader ]
  %.01821 = phi i64 [ %30, %19 ], [ 0, %.lr.ph.split.preheader ]
  %12 = load i16, ptr %.022, align 2, !tbaa !23
  %13 = getelementptr inbounds i8, ptr %.022, i64 2
  store i16 %12, ptr %13, align 2, !tbaa !26
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %19, label %16

16:                                               ; preds = %.lr.ph.split
  %17 = load ptr, ptr %9, align 8, !tbaa !27
  %18 = tail call i32 %14(ptr noundef %17, ptr noundef nonnull %0, i64 noundef %.01821) #2
  %.pre = load i16, ptr %5, align 8, !tbaa !21
  br label %19

19:                                               ; preds = %.lr.ph.split, %16
  %20 = phi i16 [ %.pre, %16 ], [ %11, %.lr.ph.split ]
  %21 = phi i32 [ %18, %16 ], [ 0, %.lr.ph.split ]
  %22 = load i32, ptr %10, align 4, !tbaa !28
  %23 = sext i32 %22 to i64
  %24 = sext i32 %21 to i64
  %25 = shl nsw i64 %24, 16
  %26 = mul nsw i64 %25, %23
  %27 = lshr i64 %26, 32
  %28 = trunc i64 %27 to i16
  store i16 %28, ptr %.022, align 2, !tbaa !23
  %29 = getelementptr inbounds i8, ptr %.022, i64 6
  %30 = add nuw nsw i64 %.01821, 1
  %31 = zext i16 %20 to i64
  %32 = icmp ult i64 %30, %31
  br i1 %32, label %.lr.ph.split, label %.loopexit, !llvm.loop !29

.loopexit:                                        ; preds = %19, %4, %1
  %33 = getelementptr inbounds i8, ptr %0, i64 112
  %34 = load i8, ptr %33, align 8
  %35 = and i8 %34, 1
  %.not20 = icmp eq i8 %35, 0
  br i1 %.not20, label %38, label %36

36:                                               ; preds = %.loopexit
  %37 = getelementptr inbounds i8, ptr %0, i64 72
  tail call void @arm_2d_dynamic_dirty_region_on_frame_start(ptr noundef nonnull %37, i8 noundef zeroext 0) #2
  br label %38

38:                                               ; preds = %36, %.loopexit
  ret void
}

declare void @arm_2d_dynamic_dirty_region_on_frame_start(ptr noundef, i8 noundef zeroext) local_unnamed_addr #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 = { "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, !14, i64 48}
!6 = !{!"histogram_t", !7, i64 0}
!7 = !{!"", !8, i64 0, !10, i64 64, !18, i64 72, !12, i64 112, !12, i64 112, !12, i64 113, !15, i64 116}
!8 = !{!"histogram_cfg_t", !9, i64 0, !16, i64 32, !14, i64 40, !17, i64 48}
!9 = !{!"", !10, i64 0, !14, i64 8, !11, i64 16, !12, i64 18, !12, i64 19, !12, i64 19, !12, i64 19, !15, i64 20, !14, i64 24}
!10 = !{!"arm_2d_size_t", !11, i64 0, !11, i64 2}
!11 = !{!"short", !12, i64 0}
!12 = !{!"omnipotent char", !13, i64 0}
!13 = !{!"Simple C/C++ TBAA"}
!14 = !{!"any pointer", !12, i64 0}
!15 = !{!"int", !12, i64 0}
!16 = !{!"", !15, i64 0, !15, i64 4}
!17 = !{!"histogram_on_get_bin_value_evt_t", !14, i64 0, !14, i64 8}
!18 = !{!"", !19, i64 0, !11, i64 32}
!19 = !{!"arm_2d_region_list_item_t", !14, i64 0, !12, i64 8, !20, i64 16, !12, i64 24}
!20 = !{!"arm_2d_region_t", !12, i64 0, !12, i64 4}
!21 = !{!6, !11, i64 16}
!22 = !{!6, !14, i64 8}
!23 = !{!24, !11, i64 0}
!24 = !{!"histogram_bin_item_t", !25, i64 0}
!25 = !{!"", !11, i64 0, !11, i64 2, !11, i64 4}
!26 = !{!24, !11, i64 2}
!27 = !{!6, !14, i64 56}
!28 = !{!6, !15, i64 116}
!29 = distinct !{!29, !30, !31}
!30 = !{!"llvm.loop.mustprogress"}
!31 = !{!"llvm.loop.unswitch.partial.disable"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass,TailCallElimPass