; LLVM IR from upupming/algorithm :: acwing/算法基础课/第四讲 数学知识/质数/868. 筛质数.ll (Z12get_primes_ei)

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

@primes = external dso_local global [1000010 x i32], align 16
@cnt = external dso_local global i32, align 4
@st = external dso_local global [1000010 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z12get_primes_ei(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 2, ptr %3, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %41, %1
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sle i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %44

11:                                               ; preds = %6
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [1000010 x i8], ptr @st, i64 0, i64 %13
  %15 = load i8, ptr %14, align 1, !tbaa !9, !range !11, !noundef !12
  %16 = trunc i8 %15 to i1
  br i1 %16, label %17, label %18

17:                                               ; preds = %11
  br label %41

18:                                               ; preds = %11
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = load i32, ptr @cnt, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr @cnt, align 4, !tbaa !5
  %22 = sext i32 %20 to i64
  %23 = getelementptr inbounds [1000010 x i32], ptr @primes, i64 0, i64 %22
  store i32 %19, ptr %23, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = add nsw i32 %24, %25
  store i32 %26, ptr %5, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %36, %18
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = icmp sle i32 %28, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %27
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %40

32:                                               ; preds = %27
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [1000010 x i8], ptr @st, i64 0, i64 %34
  store i8 1, ptr %35, align 1, !tbaa !9
  br label %36

36:                                               ; preds = %32
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = add nsw i32 %38, %37
  store i32 %39, ptr %5, align 4, !tbaa !5
  br label %27, !llvm.loop !13

40:                                               ; preds = %31
  br label %41

41:                                               ; preds = %40, %17
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %3, align 4, !tbaa !5
  br label %6, !llvm.loop !15

44:                                               ; preds = %10
  ret void
}

; 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

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 nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !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"

@primes = external dso_local local_unnamed_addr global [1000010 x i32], align 16
@cnt = external dso_local local_unnamed_addr global i32, align 4
@st = external dso_local local_unnamed_addr global [1000010 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z12get_primes_ei(i32 noundef %0) local_unnamed_addr #0 {
  %.not17 = icmp slt i32 %0, 2
  br i1 %.not17, label %._crit_edge, label %.lr.ph19.preheader

.lr.ph19.preheader:                               ; preds = %1
  %cnt.promoted = load i32, ptr @cnt, align 4, !tbaa !5
  %2 = add nuw i32 %0, 1
  %wide.trip.count = zext i32 %2 to i64
  br label %.lr.ph19

._crit_edge:                                      ; preds = %.loopexit, %1
  ret void

.lr.ph19:                                         ; preds = %.lr.ph19.preheader, %.loopexit
  %indvars.iv20 = phi i64 [ 2, %.lr.ph19.preheader ], [ %indvars.iv.next21, %.loopexit ]
  %indvars.iv = phi i64 [ 4, %.lr.ph19.preheader ], [ %indvars.iv.next, %.loopexit ]
  %3 = phi i32 [ %cnt.promoted, %.lr.ph19.preheader ], [ %15, %.loopexit ]
  %4 = getelementptr inbounds [1000010 x i8], ptr @st, i64 0, i64 %indvars.iv20
  %5 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %6 = trunc nuw i8 %5 to i1
  br i1 %6, label %.loopexit, label %7

7:                                                ; preds = %.lr.ph19
  %8 = add nsw i32 %3, 1
  store i32 %8, ptr @cnt, align 4, !tbaa !5
  %9 = sext i32 %3 to i64
  %10 = getelementptr inbounds [1000010 x i32], ptr @primes, i64 0, i64 %9
  %11 = trunc nuw nsw i64 %indvars.iv20 to i32
  store i32 %11, ptr %10, align 4, !tbaa !5
  %indvars.iv20.tr = trunc i64 %indvars.iv20 to i32
  %12 = shl i32 %indvars.iv20.tr, 1
  %.not1415 = icmp sgt i32 %12, %0
  br i1 %.not1415, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %7, %.lr.ph
  %indvars.iv22 = phi i64 [ %indvars.iv.next23, %.lr.ph ], [ %indvars.iv, %7 ]
  %13 = getelementptr inbounds [1000010 x i8], ptr @st, i64 0, i64 %indvars.iv22
  store i8 1, ptr %13, align 1, !tbaa !9
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, %indvars.iv20
  %14 = trunc nuw i64 %indvars.iv.next23 to i32
  %.not14 = icmp sgt i32 %14, %0
  br i1 %.not14, label %.loopexit, label %.lr.ph, !llvm.loop !13

.loopexit:                                        ; preds = %.lr.ph, %7, %.lr.ph19
  %15 = phi i32 [ %3, %.lr.ph19 ], [ %8, %7 ], [ %8, %.lr.ph ]
  %indvars.iv.next21 = add nuw nsw i64 %indvars.iv20, 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not = icmp eq i64 %indvars.iv.next21, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph19, !llvm.loop !15
}

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

!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 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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