; LLVM IR from huawei-noah/bolt :: compute/tensor/src (reduction)

; 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.TensorDesc = type { i32, i32, i32, [20 x i32] }
%struct.ReductionParamSpec = type { [8 x i32], i32, i32, float, i8 }
%struct.ArchInfo = type { i32, ptr }

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN6Tensor8get_descEv(ptr dead_on_unwind noalias writable sret(%struct.TensorDesc) align 4, ptr noundef nonnull align 8 dereferenceable(32)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN6Tensor5bytesEv(ptr noundef nonnull align 8 dereferenceable(32)) #1 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z33reduction_infer_forward_tmp_bytes6Tensor18ReductionParamSpecS_PjP8ArchInfo(ptr noundef %0, ptr noundef byval(%struct.ReductionParamSpec) align 8 %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %struct.TensorDesc, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  store ptr %4, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 92, ptr %10) #2
  call void @_ZN6Tensor8get_descEv(ptr dead_on_unwind writable sret(%struct.TensorDesc) align 4 %10, ptr noundef nonnull align 8 dereferenceable(32) %0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 51, ptr %11, align 4, !tbaa !9
  %15 = load ptr, ptr %9, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.ArchInfo, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = icmp eq i32 %17, 2
  br i1 %18, label %24, label %19

19:                                               ; preds = %5
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.ArchInfo, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = icmp eq i32 %22, 7
  br i1 %23, label %24, label %25

24:                                               ; preds = %19, %5
  br label %79

25:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !14
  %26 = getelementptr inbounds %struct.ReductionParamSpec, ptr %1, i32 0, i32 1
  %27 = load i32, ptr %26, align 8, !tbaa !16
  %28 = icmp sgt i32 %27, 1
  br i1 %28, label %29, label %30

29:                                               ; preds = %25
  store i32 2, ptr %12, align 4, !tbaa !14
  br label %30

30:                                               ; preds = %29, %25
  %31 = getelementptr inbounds %struct.TensorDesc, ptr %10, i32 0, i32 1
  %32 = load i32, ptr %31, align 4, !tbaa !21
  %33 = icmp eq i32 %32, 2
  br i1 %33, label %38, label %34

34:                                               ; preds = %30
  %35 = getelementptr inbounds %struct.TensorDesc, ptr %10, i32 0, i32 1
  %36 = load i32, ptr %35, align 4, !tbaa !21
  %37 = icmp eq i32 %36, 41
  br i1 %37, label %38, label %66

38:                                               ; preds = %34, %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !14
  br label %39

39:                                               ; preds = %61, %38
  %40 = load i32, ptr %13, align 4, !tbaa !14
  %41 = getelementptr inbounds %struct.ReductionParamSpec, ptr %1, i32 0, i32 1
  %42 = load i32, ptr %41, align 8, !tbaa !16
  %43 = icmp slt i32 %40, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %39
  store i32 2, ptr %14, align 4
  br label %64

45:                                               ; preds = %39
  %46 = getelementptr inbounds %struct.ReductionParamSpec, ptr %1, i32 0, i32 0
  %47 = load i32, ptr %13, align 4, !tbaa !14
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [8 x i32], ptr %46, i64 0, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !14
  %51 = icmp eq i32 %50, 1
  br i1 %51, label %59, label %52

52:                                               ; preds = %45
  %53 = getelementptr inbounds %struct.ReductionParamSpec, ptr %1, i32 0, i32 0
  %54 = load i32, ptr %13, align 4, !tbaa !14
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [8 x i32], ptr %53, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !14
  %58 = icmp eq i32 %57, -3
  br i1 %58, label %59, label %60

59:                                               ; preds = %52, %45
  store i32 2, ptr %12, align 4, !tbaa !14
  store i32 2, ptr %14, align 4
  br label %64

60:                                               ; preds = %52
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %13, align 4, !tbaa !14
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %13, align 4, !tbaa !14
  br label %39, !llvm.loop !25

64:                                               ; preds = %59, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  br label %65

65:                                               ; preds = %64
  br label %66

66:                                               ; preds = %65, %34
  %67 = call noundef i32 @_ZN6Tensor5bytesEv(ptr noundef nonnull align 8 dereferenceable(32) %0)
  %68 = call noundef i32 @_ZN6Tensor5bytesEv(ptr noundef nonnull align 8 dereferenceable(32) %2)
  %69 = icmp ugt i32 %67, %68
  br i1 %69, label %70, label %72

70:                                               ; preds = %66
  %71 = call noundef i32 @_ZN6Tensor5bytesEv(ptr noundef nonnull align 8 dereferenceable(32) %0)
  br label %74

72:                                               ; preds = %66
  %73 = call noundef i32 @_ZN6Tensor5bytesEv(ptr noundef nonnull align 8 dereferenceable(32) %2)
  br label %74

74:                                               ; preds = %72, %70
  %75 = phi i32 [ %71, %70 ], [ %73, %72 ]
  %76 = load i32, ptr %12, align 4, !tbaa !14
  %77 = mul i32 %75, %76
  %78 = load ptr, ptr %8, align 8, !tbaa !5
  store i32 %77, ptr %78, align 4, !tbaa !14
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %79

79:                                               ; preds = %74, %24
  %80 = load i32, ptr %11, align 4, !tbaa !9
  store i32 1, ptr %14, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 92, ptr %10) #2
  ret i32 %80
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = !{!"_ZTS2EE", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTS8ArchInfo", !13, i64 0, !6, i64 8}
!13 = !{!"_ZTS4Arch", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!17, !15, i64 32}
!17 = !{!"_ZTS18ReductionParamSpec", !7, i64 0, !15, i64 32, !18, i64 36, !19, i64 40, !20, i64 44}
!18 = !{!"_ZTS13ReductionMode", !7, i64 0}
!19 = !{!"float", !7, i64 0}
!20 = !{!"bool", !7, i64 0}
!21 = !{!22, !24, i64 4}
!22 = !{!"_ZTS10TensorDesc", !23, i64 0, !24, i64 4, !15, i64 8, !7, i64 12}
!23 = !{!"_ZTS8DataType", !7, i64 0}
!24 = !{!"_ZTS10DataFormat", !7, i64 0}
!25 = distinct !{!25, !26}
!26 = !{!"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"

%struct.TensorDesc = type { i32, i32, i32, [20 x i32] }
%struct.ReductionParamSpec = type { [8 x i32], i32, i32, float, i8 }

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN6Tensor8get_descEv(ptr dead_on_unwind noalias writable sret(%struct.TensorDesc) align 4, ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN6Tensor5bytesEv(ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #1 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 0, 52) i32 @_Z33reduction_infer_forward_tmp_bytes6Tensor18ReductionParamSpecS_PjP8ArchInfo(ptr noundef nonnull %0, ptr nocapture noundef readonly byval(%struct.ReductionParamSpec) align 8 %1, ptr noundef %2, ptr nocapture noundef writeonly %3, ptr nocapture noundef readonly %4) local_unnamed_addr #1 {
  %6 = alloca %struct.TensorDesc, align 4
  call void @llvm.lifetime.start.p0(i64 92, ptr nonnull %6) #2
  call void @_ZN6Tensor8get_descEv(ptr dead_on_unwind nonnull writable sret(%struct.TensorDesc) align 4 %6, ptr noundef nonnull align 8 dereferenceable(32) %0)
  %7 = load i32, ptr %4, align 8, !tbaa !5
  switch i32 %7, label %8 [
    i32 2, label %23
    i32 7, label %23
  ]

8:                                                ; preds = %5
  %9 = getelementptr inbounds i8, ptr %1, i64 32
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %.inv = icmp slt i32 %10, 2
  %spec.store.select = select i1 %.inv, i32 0, i32 2
  %11 = getelementptr inbounds i8, ptr %6, i64 4
  %12 = load i32, ptr %11, align 4
  switch i32 %12, label %.loopexit [
    i32 41, label %13
    i32 2, label %13
  ]

13:                                               ; preds = %8, %8
  %14 = icmp sgt i32 %10, 0
  br i1 %14, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %13
  %wide.trip.count = zext nneg i32 %10 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %17
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %17 ]
  %15 = getelementptr inbounds [8 x i32], ptr %1, i64 0, i64 %indvars.iv
  %16 = load i32, ptr %15, align 4, !tbaa !17
  switch i32 %16, label %17 [
    i32 1, label %.loopexit
    i32 -3, label %.loopexit
  ]

17:                                               ; preds = %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !18

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph, %17, %13, %8
  %.1 = phi i32 [ %spec.store.select, %8 ], [ %spec.store.select, %13 ], [ 2, %.lr.ph ], [ 2, %.lr.ph ], [ %spec.store.select, %17 ]
  %18 = call noundef i32 @_ZN6Tensor5bytesEv(ptr noundef nonnull align 8 dereferenceable(32) %0)
  %19 = call noundef i32 @_ZN6Tensor5bytesEv(ptr noundef nonnull align 8 dereferenceable(32) %2)
  %20 = icmp ugt i32 %18, %19
  %. = select i1 %20, ptr %0, ptr %2
  %21 = call noundef i32 @_ZN6Tensor5bytesEv(ptr noundef nonnull align 8 dereferenceable(32) %.)
  %22 = mul i32 %21, %.1
  store i32 %22, ptr %3, align 4, !tbaa !17
  br label %23

23:                                               ; preds = %5, %5, %.loopexit
  %.011 = phi i32 [ 51, %5 ], [ 0, %.loopexit ], [ 51, %5 ]
  call void @llvm.lifetime.end.p0(i64 92, ptr nonnull %6) #2
  ret i32 %.011
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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, !7, i64 0}
!6 = !{!"_ZTS8ArchInfo", !7, i64 0, !10, i64 8}
!7 = !{!"_ZTS4Arch", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !13, i64 32}
!12 = !{!"_ZTS18ReductionParamSpec", !8, i64 0, !13, i64 32, !14, i64 36, !15, i64 40, !16, i64 44}
!13 = !{!"int", !8, i64 0}
!14 = !{!"_ZTS13ReductionMode", !8, i64 0}
!15 = !{!"float", !8, i64 0}
!16 = !{!"bool", !8, i64 0}
!17 = !{!13, !13, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass