; LLVM IR from sunkafei/xcpc-algorithm-templates :: 算法/数学算法/莫比乌斯反演.ll (Z4initv)

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

@vis = external dso_local global [1000000 x i32], align 16
@prime = external dso_local global [1000000 x i32], align 16
@mu = external dso_local global [1000000 x i32], align 16
@cnt = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4initv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @llvm.memset.p0.i64(ptr align 16 @vis, i8 0, i64 4000000, i1 false)
  store i32 1, ptr getelementptr inbounds ([1000000 x i32], ptr @mu, i64 0, i64 1), align 4, !tbaa !5
  store i32 0, ptr @cnt, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 2, ptr %1, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %79, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 1000000
  br i1 %7, label %9, label %8

8:                                                ; preds = %5
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %82

9:                                                ; preds = %5
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [1000000 x i32], ptr @vis, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %24, label %15

15:                                               ; preds = %9
  %16 = load i32, ptr %1, align 4, !tbaa !5
  %17 = load i32, ptr @cnt, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr @cnt, align 4, !tbaa !5
  %19 = sext i32 %17 to i64
  %20 = getelementptr inbounds [1000000 x i32], ptr @prime, i64 0, i64 %19
  store i32 %16, ptr %20, align 4, !tbaa !5
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1000000 x i32], ptr @mu, i64 0, i64 %22
  store i32 -1, ptr %23, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %15, %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %74, %24
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = load i32, ptr @cnt, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %29, label %37

29:                                               ; preds = %25
  %30 = load i32, ptr %1, align 4, !tbaa !5
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [1000000 x i32], ptr @prime, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = mul nsw i32 %30, %34
  %36 = icmp slt i32 %35, 1000000
  br label %37

37:                                               ; preds = %29, %25
  %38 = phi i1 [ false, %25 ], [ %36, %29 ]
  br i1 %38, label %40, label %39

39:                                               ; preds = %37
  store i32 5, ptr %2, align 4
  br label %77

40:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %41 = load i32, ptr %1, align 4, !tbaa !5
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [1000000 x i32], ptr @prime, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = mul nsw i32 %41, %45
  store i32 %46, ptr %4, align 4, !tbaa !5
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [1000000 x i32], ptr @vis, i64 0, i64 %48
  store i32 1, ptr %49, align 4, !tbaa !5
  %50 = load i32, ptr %1, align 4, !tbaa !5
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [1000000 x i32], ptr @prime, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = srem i32 %50, %54
  %56 = icmp eq i32 %55, 0
  br i1 %56, label %57, label %61

57:                                               ; preds = %40
  %58 = load i32, ptr %4, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [1000000 x i32], ptr @mu, i64 0, i64 %59
  store i32 0, ptr %60, align 4, !tbaa !5
  store i32 5, ptr %2, align 4
  br label %71

61:                                               ; preds = %40
  %62 = load i32, ptr %1, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [1000000 x i32], ptr @mu, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = sub nsw i32 0, %65
  %67 = load i32, ptr %4, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [1000000 x i32], ptr @mu, i64 0, i64 %68
  store i32 %66, ptr %69, align 4, !tbaa !5
  br label %70

70:                                               ; preds = %61
  store i32 0, ptr %2, align 4
  br label %71

71:                                               ; preds = %70, %57
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %72 = load i32, ptr %2, align 4
  switch i32 %72, label %77 [
    i32 0, label %73
  ]

73:                                               ; preds = %71
  br label %74

74:                                               ; preds = %73
  %75 = load i32, ptr %3, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %3, align 4, !tbaa !5
  br label %25, !llvm.loop !9

77:                                               ; preds = %71, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %78

78:                                               ; preds = %77
  br label %79

79:                                               ; preds = %78
  %80 = load i32, ptr %1, align 4, !tbaa !5
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !11

82:                                               ; preds = %8
  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

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

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

attributes #0 = { 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 #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

@vis = external dso_local local_unnamed_addr global [1000000 x i32], align 16
@prime = external dso_local local_unnamed_addr global [1000000 x i32], align 16
@mu = external dso_local local_unnamed_addr global [1000000 x i32], align 16
@cnt = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z4initv() local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(4000000) @vis, i8 0, i64 4000000, i1 false)
  store i32 1, ptr getelementptr inbounds (i8, ptr @mu, i64 4), align 4, !tbaa !5
  store i32 0, ptr @cnt, align 4, !tbaa !5
  br label %2

1:                                                ; preds = %.critedge
  ret void

2:                                                ; preds = %0, %.critedge
  %indvars.iv27 = phi i64 [ 2, %0 ], [ %indvars.iv.next28, %.critedge ]
  %3 = phi i32 [ 0, %0 ], [ %13, %.critedge ]
  %4 = getelementptr inbounds [1000000 x i32], ptr @vis, i64 0, i64 %indvars.iv27
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %6, label %12

6:                                                ; preds = %2
  %7 = add nsw i32 %3, 1
  store i32 %7, ptr @cnt, align 4, !tbaa !5
  %8 = sext i32 %3 to i64
  %9 = getelementptr inbounds [1000000 x i32], ptr @prime, i64 0, i64 %8
  %10 = trunc nuw nsw i64 %indvars.iv27 to i32
  store i32 %10, ptr %9, align 4, !tbaa !5
  %11 = getelementptr inbounds [1000000 x i32], ptr @mu, i64 0, i64 %indvars.iv27
  store i32 -1, ptr %11, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %6, %2
  %13 = phi i32 [ %7, %6 ], [ %3, %2 ]
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %12
  %15 = getelementptr inbounds [1000000 x i32], ptr @mu, i64 0, i64 %indvars.iv27
  %wide.trip.count = zext nneg i32 %13 to i64
  %16 = trunc nuw nsw i64 %indvars.iv27 to i32
  br label %17

17:                                               ; preds = %.lr.ph, %27
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %27 ]
  %18 = getelementptr inbounds [1000000 x i32], ptr @prime, i64 0, i64 %indvars.iv
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = mul nsw i32 %19, %16
  %21 = icmp slt i32 %20, 1000000
  br i1 %21, label %22, label %.critedge

22:                                               ; preds = %17
  %23 = sext i32 %20 to i64
  %24 = getelementptr inbounds [1000000 x i32], ptr @vis, i64 0, i64 %23
  store i32 1, ptr %24, align 4, !tbaa !5
  %25 = srem i32 %16, %19
  %.not21 = icmp eq i32 %25, 0
  br i1 %.not21, label %.thread, label %27

.thread:                                          ; preds = %22
  %26 = getelementptr inbounds [1000000 x i32], ptr @mu, i64 0, i64 %23
  store i32 0, ptr %26, align 4, !tbaa !5
  br label %.critedge

27:                                               ; preds = %22
  %28 = load i32, ptr %15, align 4, !tbaa !5
  %29 = sub nsw i32 0, %28
  %30 = getelementptr inbounds [1000000 x i32], ptr @mu, i64 0, i64 %23
  store i32 %29, ptr %30, align 4, !tbaa !5
  %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 %.critedge, label %17, !llvm.loop !9

.critedge:                                        ; preds = %27, %17, %12, %.thread
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %exitcond30.not = icmp eq i64 %indvars.iv.next28, 1000000
  br i1 %exitcond30.not, label %1, label %2, !llvm.loop !11
}

; Function Attrs: mustprogress 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(readwrite, argmem: none, inaccessiblemem: 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" }
attributes #1 = { mustprogress 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass