; LLVM IR from upupming/algorithm :: acwing/算法基础课/第三讲 搜索与图论/Prim/858. Prim算法求最小生成树.ll (Z4primv)

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

@n = external dso_local global i32, align 4
@a = external dso_local global [510 x [510 x i32]], align 16
@d = external dso_local global [510 x i32], align 16
@v = external dso_local global [510 x i8], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z4primv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.memset.p0.i64(ptr align 16 @d, i8 63, i64 2040, i1 false)
  store i32 0, ptr getelementptr inbounds ([510 x i32], ptr @d, i64 0, i64 1), align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 1, ptr %1, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %77, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %39, %11
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = load i32, ptr @n, align 4, !tbaa !5
  %15 = icmp sle i32 %13, %14
  br i1 %15, label %17, label %16

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

17:                                               ; preds = %12
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [510 x i8], ptr @v, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !9, !range !11, !noundef !12
  %22 = trunc i8 %21 to i1
  br i1 %22, label %38, label %23

23:                                               ; preds = %17
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %36, label %26

26:                                               ; preds = %23
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = icmp slt i32 %30, %34
  br i1 %35, label %36, label %38

36:                                               ; preds = %26, %23
  %37 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %37, ptr %3, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %36, %26, %17
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %4, align 4, !tbaa !5
  br label %12, !llvm.loop !13

42:                                               ; preds = %16
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [510 x i8], ptr @v, i64 0, i64 %44
  store i8 1, ptr %45, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %73, %42
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = load i32, ptr @n, align 4, !tbaa !5
  %49 = icmp sle i32 %47, %48
  br i1 %49, label %51, label %50

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

51:                                               ; preds = %46
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [510 x i8], ptr @v, i64 0, i64 %53
  %55 = load i8, ptr %54, align 1, !tbaa !9, !range !11, !noundef !12
  %56 = trunc i8 %55 to i1
  br i1 %56, label %72, label %57

57:                                               ; preds = %51
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %59
  %61 = load i32, ptr %3, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [510 x [510 x i32]], ptr @a, i64 0, i64 %62
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [510 x i32], ptr %63, i64 0, i64 %65
  %67 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %60, ptr noundef nonnull align 4 dereferenceable(4) %66)
  %68 = load i32, ptr %67, align 4, !tbaa !5
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %70
  store i32 %68, ptr %71, align 4, !tbaa !5
  br label %72

72:                                               ; preds = %57, %51
  br label %73

73:                                               ; preds = %72
  %74 = load i32, ptr %5, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %5, align 4, !tbaa !5
  br label %46, !llvm.loop !15

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

77:                                               ; preds = %76
  %78 = load i32, ptr %1, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !16

80:                                               ; preds = %10
  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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #3

attributes #0 = { 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 #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { inlinehint 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 #4 = { 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}
!16 = distinct !{!16, !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"

@n = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local global [510 x [510 x i32]], align 16
@d = external dso_local global [510 x i32], align 16
@v = external dso_local local_unnamed_addr global [510 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4primv() local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(2040) @d, i8 63, i64 2040, i1 false)
  store i32 0, ptr getelementptr inbounds (i8, ptr @d, i64 4), align 4, !tbaa !5
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 1
  br i1 %2, label %.preheader, label %._crit_edge30

.preheader:                                       ; preds = %0, %._crit_edge28
  %3 = phi i32 [ %51, %._crit_edge28 ], [ %1, %0 ]
  %.01929 = phi i32 [ %52, %._crit_edge28 ], [ 1, %0 ]
  %.not21 = icmp slt i32 %3, 1
  br i1 %.not21, label %._crit_edge.thread, label %.lr.ph.preheader

._crit_edge.thread:                               ; preds = %.preheader
  store i8 1, ptr @v, align 16, !tbaa !9
  br label %._crit_edge28

.lr.ph.preheader:                                 ; preds = %.preheader
  %4 = zext nneg i32 %3 to i64
  %xtraiter = and i64 %4, 1
  %5 = icmp eq i32 %3, 1
  br i1 %5, label %._crit_edge.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %4, 2147483646
  br label %.lr.ph

._crit_edge30:                                    ; preds = %._crit_edge28, %0
  ret void

._crit_edge.unr-lcssa:                            ; preds = %50, %.lr.ph.preheader
  %.1.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.1.1, %50 ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.1, %50 ]
  %.01822.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %.1.1, %50 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.unr-lcssa
  %6 = getelementptr inbounds [510 x i8], ptr @v, i64 0, i64 %indvars.iv.unr
  %7 = load i8, ptr %6, align 1, !tbaa !9, !range !11, !noundef !12
  %8 = trunc nuw i8 %7 to i1
  br i1 %8, label %._crit_edge, label %9

9:                                                ; preds = %.lr.ph.epil
  %10 = icmp eq i32 %.01822.unr, 0
  br i1 %10, label %18, label %11

11:                                               ; preds = %9
  %12 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %indvars.iv.unr
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = sext i32 %.01822.unr to i64
  %15 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp slt i32 %13, %16
  br i1 %17, label %18, label %._crit_edge

18:                                               ; preds = %11, %9
  %19 = trunc nuw nsw i64 %indvars.iv.unr to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %11, %18, %._crit_edge.unr-lcssa
  %.1.lcssa = phi i32 [ %.1.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.01822.unr, %.lr.ph.epil ], [ %19, %18 ], [ %.01822.unr, %11 ]
  %20 = sext i32 %.1.lcssa to i64
  %21 = getelementptr inbounds [510 x i8], ptr @v, i64 0, i64 %20
  store i8 1, ptr %21, align 1, !tbaa !9
  br i1 %.not21, label %._crit_edge28, label %.lr.ph27

.lr.ph:                                           ; preds = %50, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %50 ]
  %.01822 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.1.1, %50 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %50 ]
  %22 = getelementptr inbounds [510 x i8], ptr @v, i64 0, i64 %indvars.iv
  %23 = load i8, ptr %22, align 1, !tbaa !9, !range !11, !noundef !12
  %24 = trunc nuw i8 %23 to i1
  br i1 %24, label %.lr.ph.1, label %25

25:                                               ; preds = %.lr.ph
  %26 = icmp eq i32 %.01822, 0
  br i1 %26, label %34, label %27

27:                                               ; preds = %25
  %28 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %indvars.iv
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sext i32 %.01822 to i64
  %31 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp slt i32 %29, %32
  br i1 %33, label %34, label %.lr.ph.1

34:                                               ; preds = %27, %25
  %35 = trunc nuw nsw i64 %indvars.iv to i32
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %27, %34
  %.1 = phi i32 [ %.01822, %.lr.ph ], [ %35, %34 ], [ %.01822, %27 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %36 = getelementptr inbounds [510 x i8], ptr @v, i64 0, i64 %indvars.iv.next
  %37 = load i8, ptr %36, align 1, !tbaa !9, !range !11, !noundef !12
  %38 = trunc nuw i8 %37 to i1
  br i1 %38, label %50, label %39

39:                                               ; preds = %.lr.ph.1
  %40 = icmp eq i32 %.1, 0
  br i1 %40, label %48, label %41

41:                                               ; preds = %39
  %42 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %indvars.iv.next
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = sext i32 %.1 to i64
  %45 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = icmp slt i32 %43, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %41, %39
  %49 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %50

50:                                               ; preds = %48, %41, %.lr.ph.1
  %.1.1 = phi i32 [ %.1, %.lr.ph.1 ], [ %49, %48 ], [ %.1, %41 ]
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.unr-lcssa, label %.lr.ph, !llvm.loop !13

._crit_edge28:                                    ; preds = %64, %._crit_edge.thread, %._crit_edge
  %51 = phi i32 [ %3, %._crit_edge ], [ %3, %._crit_edge.thread ], [ %65, %64 ]
  %52 = add nuw nsw i32 %.01929, 1
  %53 = icmp slt i32 %52, %51
  br i1 %53, label %.preheader, label %._crit_edge30, !llvm.loop !15

.lr.ph27:                                         ; preds = %._crit_edge, %64
  %54 = phi i32 [ %65, %64 ], [ %3, %._crit_edge ]
  %55 = phi i32 [ %66, %64 ], [ %3, %._crit_edge ]
  %indvars.iv32 = phi i64 [ %indvars.iv.next33, %64 ], [ 1, %._crit_edge ]
  %56 = getelementptr inbounds [510 x i8], ptr @v, i64 0, i64 %indvars.iv32
  %57 = load i8, ptr %56, align 1, !tbaa !9, !range !11, !noundef !12
  %58 = trunc nuw i8 %57 to i1
  br i1 %58, label %64, label %59

59:                                               ; preds = %.lr.ph27
  %60 = getelementptr inbounds [510 x i32], ptr @d, i64 0, i64 %indvars.iv32
  %61 = getelementptr inbounds [510 x [510 x i32]], ptr @a, i64 0, i64 %20, i64 %indvars.iv32
  %62 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %60, ptr noundef nonnull align 4 dereferenceable(4) %61)
  %63 = load i32, ptr %62, align 4, !tbaa !5
  store i32 %63, ptr %60, align 4, !tbaa !5
  %.pre = load i32, ptr @n, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %.lr.ph27, %59
  %65 = phi i32 [ %54, %.lr.ph27 ], [ %.pre, %59 ]
  %66 = phi i32 [ %55, %.lr.ph27 ], [ %.pre, %59 ]
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %67 = sext i32 %66 to i64
  %.not20.not = icmp slt i64 %indvars.iv32, %67
  br i1 %.not20.not, label %.lr.ph27, label %._crit_edge28, !llvm.loop !16
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { inlinehint 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" }

!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}
!16 = distinct !{!16, !14}


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